构建Home(dirty,state:_HomeState#3752d)时引发了以下ArgumentError:无效的参数

时间:2018-12-16 06:31:29

标签: dart flutter

enter image description here登录后重定向到新页面时,我得到了脏状态异常。在此主页上,我从首选项中获取信息。

class Home extends StatefulWidget{
  @override
  State<StatefulWidget> createState() => new _HomeState();
}

class _HomeState extends State<Home> {
  Drawer drawer = new Drawer();

  String strName, strEmail, strImageURL;
  int userId;
  Image imgProfile;

  @override
  void initState() {
    super.initState();
    _loadSharedPref();
  }

  //Loading Shared value on start
  _loadSharedPref() async {
    print('In Shared Profile');
    try{
      SharedPreferences prefs = await SharedPreferences.getInstance();
      strName = prefs.getString('name');
      print('Shared Profile Name '+strName);
      setState(() {
        strName = prefs.getString('name');
        userId = prefs.getInt('userId');
        strEmail = prefs.getString('email');
        strImageURL = prefs.getString('avatar');
        imgProfile = Image.network(Config.PROFILE_PIC+strImageURL);
        print('Image URL ---- '+Config.PROFILE_PIC+strImageURL);
      });
    }catch(exception){
      print('SharedProfile '+exception);
    }

  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: new Scaffold(
        backgroundColor: whiteColor,
        drawer: sideDrawer(),
        appBar: new AppBar(
          title: new Text('Test Page'),
        ),
      ),
    );
  }

  //Profile Details
  Widget profileDetail() {
    return new DrawerHeader(
        child: new Column(
          children: <Widget>[
            new CircleAvatar(
              radius: 40,
              backgroundColor : blueColor,
              backgroundImage: new NetworkImage(Config.PROFILE_PIC+strImageURL),
            ),
          ],
        )
    );
  }

  //Side Drawer
  Widget sideDrawer(){
    return new Drawer(
      child: new ListView(
        children: <Widget>[
          new Center(
            child: profileDetail(),
          ),
          new ListTile(
            leading: new Icon(Icons.lightbulb_outline),
            title: new Text('Requests'),
//          onTap: () => _onListTileTap(context),

          ),
          new ListTile(
            leading: new Icon(Icons.lightbulb_outline),
            title: new Text('Reviews'),
//            onTap: () => _onListTileTap(context),
          ),
          new ListTile(
            leading: new Icon(Icons.lightbulb_outline),
            title: new Text('Setting'),
//            onTap: () => _onListTileTap(context),
          ),
          new ListTile(
            leading: new Icon(Icons.lightbulb_outline),
            title: new Text('Tip'),
//            onTap: () => _onListTileTap(context),
          ),
          new ListTile(
            leading: new Icon(Icons.lightbulb_outline),
            title: new Text('Conditions'),
//            onTap: () => _onListTileTap(context),
          ),
          new ListTile(
            leading: new Icon(Icons.lightbulb_outline),
            title: new Text('About Us'),
//            onTap: () => _onListTileTap(context),
          ),
          new ListTile(
            leading: new Icon(Icons.lightbulb_outline),
            title: new Text('Log Out'),
//            onTap: () => _onListTileTap(context),
          ),
        ],
      ),
    );
  }
}

交叉链接https://github.com/flutter/flutter/issues/25428

3 个答案:

答案 0 :(得分:0)

您可能没有在首选项中填写“名称”,“ userId”,“头像”或“电子邮件”字段,而尝试使用SharedPreferences时却收到此错误。

答案 1 :(得分:0)

我遇到了同样的问题。检查具有共享的pref值的变量为null或对我不起作用。

在小部件中使用它们之前,请尝试检查strName,userId,strEmail,strImageURL,imgProfile是否为空。

if(strImageURL != null)
{
  print('filepaths: '+strImageURL);
}

尝试一下

  //Profile Details
  Widget profileDetail() {
    return new DrawerHeader(
        child: new Column(
          children: <Widget>[
            new CircleAvatar(
              radius: 40,
              backgroundColor : blueColor,
              backgroundImage: strImageURL ?? AssetImage('some static file path') : NetworkImage(Config.PROFILE_PIC+strImageURL),
            ),
          ],
        )
    );
  }

答案 2 :(得分:0)

每次尝试像这样从sharedPreference调用数据时,只需创建一条语句

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())