0

I am developing a social networking application using Flutter, and I have a question about a part of the screen transition/argument specification that I don't understand.

Questions to ask

I get an error like the one in the title when I put an argument (user) in Home that I put in var _routes in root.dart. What is the cause of this and what can I do about it?

Code

root.dart

class RootWidget extends StatefulWidget {
  // Receives user information from arguments
  RootWidget(this.user);
  // user information
  final User user;

  @override
  _RootWidget createState() => _RootWidget(user);
}

class _RootWidget extends State<RootWidget> {
  int _selectedIndex = 0;
  final _bottomNavigationBarItems = <BottomNavigationBarItem>[];

  // Receives user information from arguments
  _RootWidget(this.user);
  // user information
 final User user;


  //icon information
  static const _RootWidgetIcons = [
    Icons.home,
    Icons.notifications_active,
    Icons.smart_toy,
    Icons.sports_esports,
  ];
    //icon strings
  static const _RootWidgetItemNames = [
    'home',
    'notification',
    'random',
    'game',
  ];

  var _routes = [
    Home(user),
    Notice(),
    Roulette(),
    Game(),
  ];

  @override
  void initState() {
    super.initState();
    _bottomNavigationBarItems.add(_UpdateActiveState(0));
    for (var i = 1; i < _RootWidgetItemNames.length; i++) {
      _bottomNavigationBarItems.add(_UpdateDeactiveState(i));
    }
  }

home_route.dart

class Home extends StatefulWidget {
  Home(this.user);
  //user information
  final User user;
  @override
  _Home createState() => _Home(user);
}

class _Home extends State<Home> {
    //Receives user information from arguments
    _Home(this.user);
    //user information
    final User user;
    @override
        Widget build(BuildContext context) {
          return Scaffold(
            appBar: AppBar(
              title: Text('home'),
              actions: <Widget>[
                IconButton(
                  icon: Icon(Icons.close),
                  onPressed: () async {
                    //logout process
                    //Initialization of internally held login information, etc.
                    await FirebaseAuth.instance.signOut();
                    // Transition to login screen + discard chat screen
                    await Navigator.of(context).pushReplacement(
                      MaterialPageRoute(builder: (context) {
                        return LoginPage();
                      }),
                    );
                  },
                ),
              ],
            ),
            body: Center(
              //Display user information
                child: Text('Login information:${user.email}')
            ),
            floatingActionButton: FloatingActionButton(
              child: Icon(Icons.add),
              onPressed: () async {
                // Moves to the submission screen
                await Navigator.of(context).push(
                  MaterialPageRoute(builder: (context) {
                    return AddPostPage(user);
                  }),
                );
              },
            ),
          );
        }
}

Screen

The source of the transition is "root.dart" and the destination is "home_route.dart".

This is the image, but selecting the bottomNavigationBar tab will take you to the H,N,R,G screen, and "home_route.dart" is one of the four.

enter image description here

itoyen
  • 1
  • don't use a constructor _RootWidget(this.user) and final attibute and just pass the value from the getter widget, Home(widget.user)... – EdwynZN Apr 05 '22 at 22:03
  • @EdwynZN Thanks for your comment. `var _routes = [ Home(widget.user), Notice(), Roulette(), Game(), ]; ` but The instance member 'widget' can't be accessed in an initializer error occurs. Is this a mistake? – itoyen Apr 06 '22 at 22:02

0 Answers0