Flutter Instance member ‘result’ can’t be accessed using static access
You can call a function that returns a list with the calculated values and not calculate everything inside the constructor.
Example code:
class CalcCCenter {
List calculate({double x1, double y1, double x2, double y2, double x3, double y3}) {
double a = x1 * (y2 - y3) - y1 * (x2 - x3) + x2 * y3 - x3 * y2;
double b = (x1 * x1 + y1 * y1) * (y3 - y2) +
(x2 * x2 + y2 * y2) * (y1 - y3) +
(x3 * x3 + y3 * y3) * (y2 - y1);
double c = (x1 * x1 + y1 * y1) * (x2 - x3) +
(x2 * x2 + y2 * y2) * (x3 - x1) +
(x3 * x3 + y3 * y3) * (x1 - x2);
double cX = -b / (2 * a);
double cY = -c / (2 * a);
double cDiameter = ((hypotenuse(cX - x1, cY - y1)) * 2);
print('Circle diameter $cDiameter');
print('Circle center X $cX');
print('Circle center y $cY');
return [cX, cY, cDiameter];
}
}
Then in your 'onPressed' method call it like this:
onPressed: () {
setState(
() {
double x1 = double.parse(num1controller.text);
double y1 = double.parse(num2controller.text);
double x2 = double.parse(num3controller.text);
double y2 = double.parse(num4controller.text);
double x3 = double.parse(num5controller.text);
double y3 = double.parse(num6controller.text);
List result = CalcCCenter().calculate(x1: x1, y1: y1, x2: x2, y2: y2, x3: x3, y3: y3);
// Now you can access the calculated values
print('cX: ${result[0]}');
print('cY: ${result[1]}');
print('cDiameter: ${result[2]}');
},
);
},
David Vanstone
Updated on December 26, 2022Comments
-
David Vanstone over 1 year
Please go easy on me, this is my first time using flutter/dart (a couple days in), and is my first time learning a language, I'm not fully understanding of classes and functions...
Basically I have created an app that calculates the diameter of a circle from three x,y points and outputs the circle center coord's and the diameter of the circle. I had it working in one main.dart file, but it was messy, and I will be calculating lots of other things, some using this set of equations for the circle data for further calculations.
I have created a class in another file (calculator.dart) called 'CalcCCenter' here and imported that to main.dart:
import 'package:scidart/numdart.dart'; class CalcCCenter { double x1; double y1; double x2; double y2; double x3; double y3; double cX; double cY; double cDiameter; CalcCCenter( {this.x1, this.y1, this.x2, this.y2, this.x3, this.y3, this.cX, this.cY, this.cDiameter}) { double a = x1 * (y2 - y3) - y1 * (x2 - x3) + x2 * y3 - x3 * y2; double b = (x1 * x1 + y1 * y1) * (y3 - y2) + (x2 * x2 + y2 * y2) * (y1 - y3) + (x3 * x3 + y3 * y3) * (y2 - y1); double c = (x1 * x1 + y1 * y1) * (x2 - x3) + (x2 * x2 + y2 * y2) * (x3 - x1) + (x3 * x3 + y3 * y3) * (x1 - x2); cX = -b / (2 * a); cY = -c / (2 * a); print('Circle center X $cX'); print('Circle center y $cY'); cDiameter = ((hypotenuse(cX - x1, cY - y1)) * 2); print('Circle diameter $cDiameter'); } }
In main.dart I have this:
onPressed: () { setState( () { double x1 = double.parse(num1controller.text); double y1 = double.parse(num2controller.text); double x2 = double.parse(num3controller.text); double y2 = double.parse(num4controller.text); double x3 = double.parse(num5controller.text); double y3 = double.parse(num6controller.text); CalcCCenter( x1: x1, y1: y1, x2: x2, y2: y2, x3: x3, y3: y3, ); //some code here to bring the values from calulator.dart }, ); },
The inputs from the app are working correctly, the class 'CalcCCenter' in calulator.dart are taking the values and printing them (from calculator.dart). I just need to bring them (cX, cY and Cdiameter) back into main.dart to use. How would I do this with a function? I have tried a few things, but keep getting the error "Flutter Instance member ‘result’ can’t be accessed using static access". Sorry if this may seem basic to many, but I'm just starting out.
-
David Vanstone over 3 yearsBrilliant, thank you for that. It all makes sense now! Will give it a try after work.
-
Luis Cruz over 3 yearsIf it works, don't forget to accept the answer as correct.