'Flutter. Get data from Navigator.of(context)
I'm beginner of flutter and trying to make app now.
I have 3 pages.
First page has a ElevatedButton with Navigator.of(context).pushnamed('/second') Second page has a textEditingController, and the value of textEditingController is [int] Evalue. And I change screen by Navigator.of(context).pushReplacementNamed('/third, arguments : Evalue) Last page has a ElevatedButton to show the Evalue, and Change screen by Navigator.of(context).pop(Evalue) (I also got Evalue from Secondpage by ModalRoute.of(context)!.setting.arguments)
but when I come to first page, I don't get Evalue from Lastpage. I got Null value.
this is my code in first page. onPressed: () async {
final value = await Navigator.of(context).pushNamed('/second');
EveryTime I get null...
What is problem guys? Is there anyway to get data from third page to first page?
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'database.dart';
import 'alphabeta.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
Future<Database> database = initDatabase();
return MaterialApp(
title: 'NoiceAnalytics',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: '/',
routes: {'/': (context) => WidgetApp(database),
'/first': (context) => first(database),
'/second': (context) => second(database),
'/third': (context) => third(database),
'
}
);
}
class WidgetApp extends StatefulWidget {
final Future<Database> db;
WidgetApp(this.db);
@override
_WidgetApp createState() => _WidgetApp();
}
class _WidgetApp extends State<WidgetApp> {
@override
Widget build(BuildContext context) {
database? db4;
return Scaffold(
body: Container(
child: Center(
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(15),
child: SizedBox(width: 250, height: 80,
child: ElevatedButton(
child: Text(
'Noise', style: TextStyle(fontSize: 20),),
onPressed: () async {
final db3 = await Navigator.of(context).pushNamed('/second');
},
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20))),
)
)
),
}
class second extends StatefulWidget {
final Future<Database> db;
second(this.db);
@override
State<StatefulWidget> createState() => _second();
}
class _second extends State<Backgroundnoise>{
TextEditingController? bgnController;
TextEditingController? measuretimeController;
@override
void initState(){
super.initState();
bgnController = new TextEditingController();
measuretimeController = new TextEditingController();
}
@override
Widget build(BuildContext context){
return Scaffold(
body: Container(
child: Center(
child: SingleChildScrollView(scrollDirection: Axis.vertical,
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 100 ,bottom: 15),
child: Text('Background', style: TextStyle(fontSize: 20)),
),
Padding(
padding: EdgeInsets.all(15),
child: SizedBox(width: 250, height: 80,
child: TextField(
controller: bgnController,
decoration: InputDecoration(labelText: 'enter the value', hintText: 'Ex)80.5'),
keyboardType: TextInputType.number,
)
)
),
Padding(
padding: EdgeInsets.all(15),
child: Text('Time', style: TextStyle(fontSize: 20)),
),
padding: EdgeInsets.all(15),
child: SizedBox(width: 250, height: 80,
child: TextField(
controller: measuretimeController,
decoration: InputDecoration(labelText: 'enter the value', hintText: 'Ex)80.5'),
keyboardType: TextInputType.number,
)
)
),
Padding(
padding: EdgeInsets.all(15),
child: SizedBox(width: 250, height: 80,
child: ElevatedButton(
child: Text('result', style: TextStyle(fontSize: 20),), onPressed: (){
database db1 = database(
bgn: bgnController!.value.text,
measuretime: measuretimeController!.value.text,
Navigator.of(context).pushReplacementNamed('/third', arguments: db1);
},
style: ElevatedButton.styleFrom(shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(50))),
),
),
)
],
mainAxisAlignment: MainAxisAlignment.center,
),
),
),
),
);
}
}
class third extends StatefulWidget {
final Future<Database> db;
third(this.db);
@override
State<StatefulWidget> createState() => _third();
}
class _third extends State<Calculation> {
@override
Widget build(BuildContext context) {
final database db2 = ModalRoute.of(context)!.settings.arguments as database;
return Scaffold(
body: Container(
child: Center(
child: Column(children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 15 ,bottom: 15),
child: Text('S.sound', style: TextStyle(fontSize: 20)),
),
Padding(
padding: EdgeInsets.all(15),
child: SizedBox(width: 250, height: 80,
child: ElevatedButton(
child: Text(db2.bgn.toString(), style: TextStyle(fontSize: 20),),
onPressed: () {},
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50))),
),
),
),
Padding(
padding: EdgeInsets.all(15),
child: Text('Level', style: TextStyle(fontSize: 20)),
),
Padding(
padding: EdgeInsets.all(15),
child: SizedBox(width: 250, height: 80,
child: ElevatedButton(
child: Text(db2.measuretime.toString(), style: TextStyle(fontSize: 20),),
onPressed: () {},
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50))),
),
),
),
Padding(
padding: EdgeInsets.only(top: 30),
child:
Row(children: <Widget>[
Padding(
padding: EdgeInsets.all(5),
child:
SizedBox(width: 200, height: 80,
child: ElevatedButton(
child: Text('Save and Quit', style: TextStyle(fontSize: 20),),
onPressed: (){
Navigator.of(context).pop(db2);
},
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50)))
)
)
),
],mainAxisAlignment: MainAxisAlignment.center
)
)
],
mainAxisAlignment: MainAxisAlignment.center,
),
)
)
);
}
}
and where database.dart is
class database{
int? id;
String? bgn;
String? measuretime;
database({this.id, this.bgn ,this.measuretime});
Map<String, dynamic> toMap(){
return{
'id': id, 'bgn': bgn, 'measuretime': measuretime
};
}
}
Solution 1:[1]
I think your mistake is how you retrieve your value from the new screen you pushed.
Currently, from what I understood, you're using:
final value = await Navigator.of(context).pushNamed('/second');
which indeed, will give you a null value.
You need to use the settings.argument in the new screen.
ModalRoute.of(context)!.settings.arguments
You can find more about it here:
https://docs.flutter.dev/cookbook/navigation/passing-data
https://docs.flutter.dev/cookbook/navigation/navigate-with-arguments
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BLKKKBVSIK |
