i get the problem in this source code. And I get problems like this, and I’ve tried to make a declaration but didn’t find it The named parameter ‘message’ isn’t defined.at [97:53]. And i used flutter at flutlab.io
erorr code
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Passing Variable Demo',
home: FirstPage(),
);
}
}
class FirstPage extends StatefulWidget {
@override
_FirstPageState createState() => _FirstPageState();
}
class _FirstPageState extends State<FirstPage> {
TextEditingController _textEditingController = TextEditingController();
String _message = '';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('First Page'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Message: $_message'),
TextField(
controller: _textEditingController, //Tampilan kotak text editor
decoration: InputDecoration(
hintText: 'Enter your message',
),
),
ElevatedButton(
child: Text('Go to Second Page'),
onPressed: () async {
final result = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondPage(message: _message),
),
);
setState(() {
_message = result ??
''; //Ini untuk menggantikan text message yang kosong
});
},
),
],
),
),
);
}
}
class SecondPage extends StatefulWidget {
final String message;
SecondPage({required this.message});
@override
_SecondPageState createState() => _SecondPageState();
}
class _SecondPageState extends State<SecondPage> {
String _message2 = '';
TextEditingController _textEditingController =
TextEditingController(); //fungsi untuk data edit text
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Page'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Message: $_message2'),
TextField(
controller: _textEditingController, //Tampilan kotak text editor
decoration: InputDecoration(
hintText: 'Enter your message',
),
),
ElevatedButton(
child: Text('Go back to First Page'),
onPressed: () async {
final result = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => FirstPage(message: _message2),
),
);
setState(() {
_message2 = result ??
''; //Ini untuk menggantikan text message yang kosong
});
},
),
],
),
),
);
}
}
I’ve tried declaring a message but there is still a problem. I hope to solve this code,
2
Answers
Replace your
FirstPage
class with the below code.Not sure if this is what you want. But I played around with your code a bit. I turned FirstPage and SecondPage into a single widget, since they are almost identical, and adding an enum for specifying what should be on a page and what the next page would be. Maybe this is not what you need, but hopefully you can learn from it and apply some of the logic to your own code: