Flutter use one Variable in different classes Error: Getter not found: 'allJobs'
589
You can copy paste run full code below
Step 1: You can use GlobalKey
and pass to Muensterboerse(key: _key1)
Step 2: In senddata()
, do (_key1.currentWidget as Muensterboerse).allJobs;
code snippet
GlobalKey _key1 = GlobalKey();
...
class Muensterboerse extends StatelessWidget {
Muensterboerse({Key key}) : super(key: key);
...
Future<dynamic> senddata() async {
int allJobs = (_key1.currentWidget as Muensterboerse).allJobs;
print(allJobs);
...
Muensterboerse(key: _key1),
output of senddata()
I/flutter (22480): 1
full code
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
GlobalKey _key1 = GlobalKey();
class Muensterboerse extends StatelessWidget {
Muensterboerse({Key key}) : super(key: key);
int allJobs = 1;
@override
Widget build(BuildContext context) {
return Column(
children: [
Text("$allJobs"),
],
);
}
}
Future<dynamic> senddata() async {
int allJobs = (_key1.currentWidget as Muensterboerse).allJobs;
print(allJobs);
/*final response = await http.post(
"https://www.quyre.de/2/Muensterboerse.N.php", body: {
"status": allJobs
});*/
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() async{
await senddata();
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Muensterboerse(key: _key1),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
![Charles Down](https://lh6.googleusercontent.com/-_quxj4J02e8/AAAAAAAAAAI/AAAAAAAAAAA/AMZuuck4aBeONqJq31Ob0mRpVNoGB35YyQ/photo.jpg?sz=256)
Author by
Charles Down
Updated on December 25, 2022Comments
-
Charles Down over 1 year
I have a Variable in one Class but and i want to use it in all. In this Example is the allJobs Variable which is declared in Muesnterboerse ore MuensterboerseAAAngebote and i want to use it in senddate().
class Muensterboerse extends StatelessWidget { var allJobs = 1; @override Widget build(BuildContext context) { return new MaterialApp( debugShowCheckedModeBanner: false, title: 'Flutter App with MYSQL', home: new MyHomePage(), ); } } class MuensterboerseAAAngebote extends StatelessWidget { var allJobs = 0; @override Widget build(BuildContext context) { return new MaterialApp( debugShowCheckedModeBanner: false, title: 'Flutter App with MYSQL', home: new MyHomePage(), ); } } Future<dynamic> senddata() async { final response = await http.post( "https://www.bumsbirnbe.php", body: { "status": allJobs, }); var datauser = json.decode(response.body); String jsonsDataString = datauser.toString(); dynamic jsonData = jsonDecode(jsonsDataString); print(jsonData); return jsonData; }
Update Now i added your changes to my code but i get the
Error: Unhandled Exception: NoSuchMethodError: The getter 'allJobs' was called on null.
This is my whole code:
import 'dart:async'; import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; GlobalKey _key1 = GlobalKey(); class Muensterboerse extends StatelessWidget { Muensterboerse({Key key}) : super(key: key); int allJobs = 1; @override Widget build(BuildContext context) { return new MaterialApp( debugShowCheckedModeBanner: false, title: 'Flutter App with MYSQL', home: new MyHomePage(), ); } } class AAAngebote extends StatelessWidget { AAAngebote({Key key}) : super(key: key); int allJobs = 2; @override Widget build(BuildContext context) { return new MaterialApp( debugShowCheckedModeBanner: false, title: 'Flutter App with MYSQL', home: new MyHomePage(), ); } } Future<dynamic> senddata() async { int allJobs = (_key1.currentWidget as Muensterboerse).allJobs; print(allJobs); final response = await http.post( "https://www.Bumsbirne.php", body: { "status": allJobs, }); var datauser = json.decode(response.body); String jsonsDataString = datauser.toString(); dynamic jsonData = jsonDecode(jsonsDataString); print(jsonData); return jsonData; } class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { dynamic jsonData; callSendData() async { jsonData = await senddata(); setState(() {}); } //lol @override void initState() { callSendData(); } @override Widget build(BuildContext context) { return Scaffold( body: jsonData == null ? Center(child: CircularProgressIndicator()) : ListView.builder( padding: const EdgeInsets.all(16.0), itemCount: jsonData == null ? 0 : jsonData.length, itemBuilder: (context, index) { return ListTile( leading: CircleAvatar( backgroundImage: NetworkImage('https://kinsta.com/de/wpcontent/uploads/sites/5/2019/09/wordpress-loggst-url-1024x512.jpg'), radius: 27, ), title: Text( jsonData[index]["titel"], ), subtitle: Text(jsonData[index]["nam_ersteller"]), trailing: Text( '25 Km', style: TextStyle(color: Colors.grey, fontSize: 12, decoration: TextDecoration.none, fontFamily: 'Roboto',), ), onTap: () { Navigator.push(context, new MaterialPageRoute(builder: (context) => DetailPage())); }, ); // return _buildRow(data[index]); })); } } class DetailPage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Der Job'), ), ); } }
-
Charles Down over 3 yearsI tried you code but it evolved into sone issues could you help me with the new problem?
-
chunhunghan over 3 yearsIn your edited code , you did not use Muensterboerse(key: _key1) in MyHomePage. please copy paste and run my full code again to check it.
-
chunhunghan over 3 yearsboth Muensterboerse and AAAngebote call MyHomePage, what is your actual entry point? please post reproduce code with MyApp and main