skip to Main Content

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


  1. Replace your FirstPage class with the below code.

    class FirstPage extends StatefulWidget {
      String message;
       FirstPage({required this.message,Key? key}) : super(key: key);
    
      @override
      State<FirstPage> createState() => _FirstPageState();
    }
    
    
    Login or Signup to reply.
  2. 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:

    import 'package:flutter/material.dart';
    
    void main() => runApp(const MyApp());
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return const MaterialApp(
          debugShowCheckedModeBanner: false,
          title: 'Passing Variable Demo',
          home: PageWidget(page: Page.first),
        );
      }
    }
    
    class PageWidget extends StatefulWidget {
      final Page page;
      final String previousMessage;
    
      const PageWidget({super.key, required this.page, this.previousMessage = ''});
    
      @override
      State<PageWidget> createState() => _PageWidgetState();
    }
    
    class _PageWidgetState extends State<PageWidget> {
      final TextEditingController _textEditingController = TextEditingController();
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.page.name),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text('Message: ${widget.previousMessage}'),
                TextField(
                  controller: _textEditingController,
                  decoration: const InputDecoration(
                    hintText: 'Enter your message',
                  ),
                ),
                ElevatedButton(
                  child: Text('Go to ${widget.page.nextPage().name}'),
                  onPressed: () async {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => PageWidget(
                            page: widget.page.nextPage(),
                            previousMessage: _textEditingController.text),
                      ),
                    );
                  },
                ),
              ],
            ),
          ),
        );
      }
    }
    
    enum Page {
      first('First Page'),
      second('Second Page');
    
      const Page(this.name);
    
      final String name;
    
      Page nextPage() {
        switch (this) {
          case Page.first:
            return Page.second;
          case Page.second:
            return Page.first;
        }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search