El Hombre Sin Nombre
El Hombre Sin Nombre

Reputation: 3102

Flutter - Send params in Bottom Navigation Bar

I have two different pages. One of these is a form, the other is a list of elements. If you swipe one of the elements to left can edit. I need to send the element data (from the list) to first page (the form)

When the application starts the data is null, if it come from the element list isn´t null.

The navigation bar code is:

import 'package:flutter/material.dart';
import 'package:datameter/screens/configuration/form/partials/home_page.dart';
import 'package:datameter/screens/configuration/list/datameters.dart';
import 'package:datameter/locations/locations.dart';

class Navigation extends StatefulWidget {

 final item;

  Navigation(
      {Key key,
      this.item})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _NavigationState();
  }
}

class _NavigationState extends State<Navigation> {

  int currentIndex = 0;
  List<Widget> children = [
    HomePageScreen(datameter: widget.item), //ERRROR HERE
    DatametersPageScreen(),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: children[currentIndex],
      bottomNavigationBar: new Theme(
          data: Theme.of(context).copyWith(
            primaryColor: Colors.blue[700],
            textTheme: Theme.of(context)
                .textTheme
                .copyWith(caption: new TextStyle(color: Colors.black)),
          ),
          child: BottomNavigationBar(
            onTap: onTabTapped,
            currentIndex: currentIndex,
            items: [
              BottomNavigationBarItem(
                icon: Icon(Icons.add),
                title: Text(DemoLocalizations.of(context).text('new-device')),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.list),
                title: Text(DemoLocalizations.of(context).text('show-all')),
              ),
            ],
          )),
    );
  }

  void onTabTapped(int index) {
    setState(() {
      currentIndex = index;
    });
  }
}

When I try to send params from elements list (using navigation bottom bar) to the form, returns

Static only members can be accessed in initializers.

Does anybody know how to fix this?

Upvotes: 3

Views: 4294

Answers (1)

Manish Dhruw
Manish Dhruw

Reputation: 803

_NavigationState Object is not constructed so you cannot access "widget" getter yet because its not initialized and its not static property either.

change

 List<Widget> children = [
    HomePageScreen(datameter: widget.item), //ERRROR HERE
    DatametersPageScreen(),
  ];

to

 List<Widget> _children() =>
 [   
    HomePageScreen(datameter: widget.item),
    DatametersPageScreen(),
 ];

and then in your build change

@override
Widget build(BuildContext context)
{
    final List<Widget> children = _children();

    return Scaffold
    (
        //code
        body: children[currentIndex],
        //code
    );
}

Upvotes: 9

Related Questions