4
votes

I having trouble with passing parameter between classes. When i wanted to pass email from MyScreen classes to my TabScreen it shows:

A [State] object's configuration is the corresponding [StatefulWidget] instance. This property is initialized by the framework before calling [initState]. If the parent updates this location in the tree to a new widget with the same [runtimeType] and [Widget.key] as the current configuration, the framework will update this property to refer to the new widget and then call [didUpdateWidget], passing the old configuration as an argument.

Not sure how to handle this passing variables to a state class. I can use '${widget.email}' but only in widget array. Inside List tabs, I really have no idea how. Quite new to flutter way of doing things...am i doing it wrongs somewhere?

import 'package:flutter/material.dart';
import 'package:my_helper/tab_screen2.dart';
import 'package:my_helper/tab_screen3.dart';
import 'tab_screen.dart';

class MainScreen extends StatefulWidget {
  final String email; //<-managed to get this variable from previous screen

MainScreen({Key key,this.email}) : super(key: key);

@override
_MainScreenState createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
 int currentTabIndex = 0;

List<Widget> tabs = [
  TabScreen("Home", widget.email), //<- ERROR this widget.email shows error "only static member can be ...."
  TabScreen2("Message"),
  TabScreen3("Profile")
];

String $pagetitle = "Home";

onTapped(int index) {
 setState(() {
  currentTabIndex = index;
 });
}

@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text($pagetitle),
  ),
  body: tabs[currentTabIndex],
  bottomNavigationBar: BottomNavigationBar(
    onTap: onTapped,
    currentIndex: currentTabIndex,
    items: [
      BottomNavigationBarItem(
        icon: Icon(Icons.search),
        title: Text("Jobs"),
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.mail),
        title: Text("Messages"),
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.person),
        title: Text("Profile"),
      )
    ],
   ),
  );
 }
}

my TabScreen

 import 'package:flutter/material.dart';
 import 'package:http/http.dart' as http;

 class TabScreen extends StatelessWidget {
 final String apptitle,email;

  TabScreen(this.apptitle,this.email);

 @override
 Widget build(BuildContext context) {
 return  Column(
   children: <Widget>[
     Text(email)
   ],
  );
 }
}
1
Did you hate initState? You could do initialization in that function. - Tokenyet
care to share how? - Hanis

1 Answers

4
votes

You need to initialize tabs in the initState() method. Change your _MainScreenState class to match this:

class _MainScreenState extends State<MainScreen> {
  int currentTabIndex = 0;

  List<Widget> tabs;

  String $pagetitle = "Home";

  onTapped(int index) {
    setState(() {
      currentTabIndex = index;
    });
  }

  @override
  void initState() {
    super.initState();
    tabs = [
      TabScreen("Home", widget.email),
      TabScreen2("Message"),
      TabScreen3("Profile"),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text($pagetitle),
      ),
      body: tabs[currentTabIndex],
      bottomNavigationBar: BottomNavigationBar(
        onTap: onTapped,
        currentIndex: currentTabIndex,
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.search),
            title: Text("Jobs"),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.mail),
            title: Text("Messages"),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            title: Text("Profile"),
          )
        ],
      ),
    );
  }
}