Flutter:第一次在第二页上传入的参数值为null

阿拉比布

Flutter:第一次在第二页上传入的参数值为null。如果我进行热装,则此值不为null。

也许是因为TabBar的缘故,但是如何在使用TabBar时第一次获得实际价值?

请帮助我找到解决问题的方法

第一页。而且idBou在这里不为null

class BoutiquePage extends StatefulWidget {
  int idboutique;

  BoutiquePage({this.idboutique});

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

class _BoutiquePageState extends State<BoutiquePage> {

  SharedPreferences sharedPreferences;
 
  
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

  String idBou ;
  String nomAbon ;
  String prenomAbon ;
  String telAbon ;
  String addAbon;
  String nomBou;
  String ville;
  String pays;

  String lien_photo_bout;


  bool _isLoading = false;


  String _errorText;

  get article_afficher333 => article_afficher333;

  @override
  void initState() { 
    setState((){ 
      getShaerInstance();
    }); 
    super.initState();
  }
  getShaerInstance() async {
    sharedPreferences = await SharedPreferences.getInstance(); 
  }


    Future<Map<String, dynamic>> getAllStoreInfoData2(int idboutique) async {
      final response = await http.get(http://xxxxxx.com+"boutique/home?id_bout="+idboutique.toString()); 
      if (response.statusCode == 200) {
        final jsonResponse = json.decode(response.body);
        BoutiqueData myData = new BoutiqueData.fromJson(jsonResponse);
        
        Map<String, dynamic> boutiqueinfodata = Map();
        boutiqueinfodata["boutiqueType"] = myData.v_boutique.typeBoutique; 
        return boutiqueinfodata;

      } else {
        throw Exception("Failed to load Data");
      }
    }

   
  //SharedPreferences

  @override
  Widget build(BuildContext context) {

    Widget myboutiqueinfodata = FutureBuilder(
      future: getAllStoreInfoData2(widget.idboutique),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          Map<String, dynamic> alldata = snapshot.data;          
          typeBoutique = alldata["boutiqueType"];        
          List<ListeInfoBoutique> boutiqueInfoData = alldata["boutiqueInfoData"];   
           
            for (var i = 0; i < boutiqueInfoData.length; i++) {
              idBou = boutiqueInfoData[i].idBou;
              nomAbon = boutiqueInfoData[i].nomAbon;
              prenomAbon = boutiqueInfoData[i].prenomAbon;
              telAbon = boutiqueInfoData[i].telAbon;
              addAbon = boutiqueInfoData[i].addAbon; 
            }

            return   Scaffold(
                body: ListView(
                  children: <Widget>[
                     
                  ],
                ),
              );



        }else if (snapshot.hasError) {
          return Container(
            child: Center(
              child: Text(AppLocalizations.of(context)
                      .translate('_MSG_ER_CONNEXION_CHARGE_DATA'), 
                                  style: TextStyle(
                                    fontSize: 18,
                                    fontFamily: 'Questrial'
                                  ),),
            ),
          );
        }
        return new Center(
          child: CircularProgressIndicator(),
        );
      });

    return DefaultTabController(
      length: 5,
      child: Scaffold(
        key: _scaffoldKey,
        appBar: AppBar(
            title: Text(AppLocalizations.of(context)
                        .translate('_MY_STORE'), style: TextStyle(color: Colors.white, fontFamily: "Questrial"),),
            iconTheme: new IconThemeData(color: Color(0xFFFFFFFF)),
            actions: <Widget>[

              //
            ],
            bottom: TabBar(
              isScrollable: true,
              indicatorColor: Colors.white,
              indicatorWeight: 5.0,
              //onTap: (){},
              tabs: <Widget>[
                Tab(
                  child: Container(
                    child: Text(AppLocalizations.of(context)
                        .translate('_STORE_INFO'), style: TextStyle(color: Colors.white, fontSize: 18.0, fontFamily: 'Questrial'),),
                  ),
                ),
                Tab(
                  child: Container(
                    child: Text(AppLocalizations.of(context)
                        .translate('_MY_ARTICLES'), style: TextStyle(color: Colors.white, fontSize: 18.0, fontFamily: 'Questrial'),),
                  ),
                ),
              ],
            ),
        ),
        body: TabBarView(
          children: <Widget>[
                myboutiqueinfodata, 
                MesArticles(idboutique: idBou),
              ],
            ),
          ),
        );
      }

    }

第一次打印为null且热重新加载后为secode的页面不为null


class MesArticles extends StatefulWidget {
  final String idboutique;
  const MesArticles({Key key, this.idboutique}) : super(key: key);

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

class _MesArticleState extends State<MesArticles> {

  @override
  Widget build(BuildContext context) {
   print(widget.idboutique.toString()); // it print null for fist time. After hot reload it become not null
    return Scaffold(
      body: Text(widget.idboutique.toString()), //<== it print null for fist time.
    );
  }
}
穆罕默德·耶尔马兹(Mehmet Yilmaz)

试试这个代码。我认为Future builder是抛出null的原因。

class BoutiquePage extends StatefulWidget {
  int idboutique;

  BoutiquePage({this.idboutique});

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

class _BoutiquePageState extends State<BoutiquePage> {

  SharedPreferences sharedPreferences;
 
  
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

  String idBou ;
  String nomAbon ;
  String prenomAbon ;
  String telAbon ;
  String addAbon;
  String nomBou;
  String ville;
  String pays;

  String lien_photo_bout;


  bool _isLoading = false;
  bool _isLoaded = false;
  bool _isError = false;


  String _errorText;

  get article_afficher333 => article_afficher333;

  @override
  void initState() { 
    setState((){ 
      getShaerInstance();
    }); 
    super.initState();
  }
  getShaerInstance() async {
    sharedPreferences = await SharedPreferences.getInstance(); 
  }


    Future<Map<String, dynamic>> getAllStoreInfoData2(int idboutique) async {
      final response = await http.get("http://xxxxxx.com/" + "boutique/home?id_bout="+idboutique.toString()); 
        if (response.statusCode == 200) {
        final jsonResponse = json.decode(response.body);
        BoutiqueData myData = new BoutiqueData.fromJson(jsonResponse);
        
        Map<String, dynamic> boutiqueinfodata = Map();
        boutiqueinfodata["boutiqueType"] = myData.v_boutique.typeBoutique; 
        return boutiqueinfodata;

      } else {
        throw Exception("Failed to load Data");
      }
    }

   
  //SharedPreferences

  initDataFromServer(idboutique) async {

    try {


    var alldata = await  getAllStoreInfoData2(idboutique);         
          var typeBoutique = alldata["boutiqueType"];        
          List<ListeInfoBoutique> boutiqueInfoData = alldata["boutiqueInfoData"];   
           
            for (var i = 0; i < boutiqueInfoData.length; i++) {
              idBou = boutiqueInfoData[i].idBou;
              nomAbon = boutiqueInfoData[i].nomAbon;
              prenomAbon = boutiqueInfoData[i].prenomAbon;
              telAbon = boutiqueInfoData[i].telAbon;
              addAbon = boutiqueInfoData[i].addAbon; 
            }

        setState(() {
          _isLoaded = true;
          _isLoading = false;
          _isError = false;
        });

    } catch (e) {
      print(e);
      setState(() {
        _isLoaded = true;
        _isLoading = true;
        _isError = true;
      });
    }
    
  }

  @override
  Widget build(BuildContext context) {
    if (_isLoaded == false && _isLoading == false) {
      _isLoading = true;
      initDataFromServer(widget.idboutique);
    }

    return DefaultTabController(
      length: 5,
      child: Scaffold(
        key: _scaffoldKey,
        appBar: AppBar(
            title: Text(AppLocalizations.of(context)
                        .translate('_MY_STORE'), style: TextStyle(color: Colors.white, fontFamily: "Questrial"),),
            iconTheme: new IconThemeData(color: Color(0xFFFFFFFF)),
            actions: <Widget>[

              //
            ],
            bottom: TabBar(
              isScrollable: true,
              indicatorColor: Colors.white,
              indicatorWeight: 5.0,
              //onTap: (){},
              tabs: <Widget>[
                Tab(
                  child: Container(
                    child: Text(AppLocalizations.of(context)
                        .translate('_STORE_INFO'), style: TextStyle(color: Colors.white, fontSize: 18.0, fontFamily: 'Questrial'),),
                  ),
                ),
                Tab(
                  child: Container(
                    child: Text(AppLocalizations.of(context)
                        .translate('_MY_ARTICLES'), style: TextStyle(color: Colors.white, fontSize: 18.0, fontFamily: 'Questrial'),),
                  ),
                ),
              ],
            ),
        ),
        body: TabBarView(
          children: <Widget>[
                _isLoaded == false ? Center(
          child: CircularProgressIndicator(),
        ): (_isError == false ? Text("Your data Loaded") : Text("Error")),
        
                _isLoaded == false ? Center(
          child: CircularProgressIndicator(),
        ): (_isError == false ? MesArticles(idboutique: idboutique) : Text("Error")), 
                
              ],
            ),
          ),
        );
      }

    }
class MesArticles extends StatefulWidget {
  final String idboutique;
  const MesArticles({Key key, this.idboutique}) : super(key: key);

  @override
  _MesArticleState createState() => _MesArticleState(idboutique: idboutique);
}

class _MesArticleState extends State<MesArticles> {
  final String idboutique;

  _MesArticleState({this.idboutique});

  @override
  Widget build(BuildContext context) {
   print(widget.idboutique.toString()); // it print null for fist time. After hot reload it become not null
    return Scaffold(
      body: Text(widget.idboutique.toString()),
    );
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

方法运行两次之前的给定方法。第一次捕获null,第二次捕获字符串参数

来自分类Dev

模态显示第一次加载为空,第二次未加载

来自分类Dev

模态显示第一次加载为空,第二次未加载

来自分类Dev

第一次和第二次之间的速度差

来自分类Dev

函数的第一次调用与第二次调用的性能

来自分类Dev

在第二次执行时暂停函数的第一次执行(Python)

来自分类Dev

.on更改功能是第一次而不是第二次起作用?

来自分类Dev

preventDefault第一次使用,但第二次不起作用

来自分类Dev

.NET第一次成功后进行第二次Ajax调用

来自分类Dev

第一次和第二次之间的速度差

来自分类Dev

浮动div的高度在第一次是零,但在第二次是可以

来自分类Dev

毕加索不缓存第一次加载,但第二次没问题

来自分类Dev

第一次登录无效,但第二次登录有效

来自分类Dev

查询正在第二次尝试但不是第一次尝试

来自分类Dev

视图第一次旋转,但不是第二次

来自分类Dev

从第一次出现到第二次解析日志

来自分类Dev

如何第一次插入并第二次更新

来自分类Dev

FCM Token 第一次不生成,第二次生成

来自分类Dev

AdsConnection 在第二次连接时抛出 EntryPointNotFoundException 但第一次工作

来自分类Dev

查找未传入数组 PHP 的第一次

来自分类Dev

列表视图中的第二项无法正确呈现第一次

来自分类Dev

在 R 中,从第二列的第一次出现更改数据框列

来自分类Dev

点击事件:如何为第一次点击设置事件,为第二次点击设置另一个事件

来自分类Dev

sed替换第一次出现参数的所有内容,而第二次出现的内容则不替换任何内容

来自分类Dev

FPDF第二页与第一页不匹配

来自分类Dev

映射并发送第一页到第二页中的数据列表

来自分类Dev

如果创建了第二页,则在第一页上隐藏页脚

来自分类Dev

第一次保存到表但第二次不保存会导致错误:sqlalchemy中的NOT NULL约束失败

来自分类Dev

当第一页与第二页具有相同的字段时,等待第二页加载水豚的正确方法

Related 相关文章

  1. 1

    方法运行两次之前的给定方法。第一次捕获null,第二次捕获字符串参数

  2. 2

    模态显示第一次加载为空,第二次未加载

  3. 3

    模态显示第一次加载为空,第二次未加载

  4. 4

    第一次和第二次之间的速度差

  5. 5

    函数的第一次调用与第二次调用的性能

  6. 6

    在第二次执行时暂停函数的第一次执行(Python)

  7. 7

    .on更改功能是第一次而不是第二次起作用?

  8. 8

    preventDefault第一次使用,但第二次不起作用

  9. 9

    .NET第一次成功后进行第二次Ajax调用

  10. 10

    第一次和第二次之间的速度差

  11. 11

    浮动div的高度在第一次是零,但在第二次是可以

  12. 12

    毕加索不缓存第一次加载,但第二次没问题

  13. 13

    第一次登录无效,但第二次登录有效

  14. 14

    查询正在第二次尝试但不是第一次尝试

  15. 15

    视图第一次旋转,但不是第二次

  16. 16

    从第一次出现到第二次解析日志

  17. 17

    如何第一次插入并第二次更新

  18. 18

    FCM Token 第一次不生成,第二次生成

  19. 19

    AdsConnection 在第二次连接时抛出 EntryPointNotFoundException 但第一次工作

  20. 20

    查找未传入数组 PHP 的第一次

  21. 21

    列表视图中的第二项无法正确呈现第一次

  22. 22

    在 R 中,从第二列的第一次出现更改数据框列

  23. 23

    点击事件:如何为第一次点击设置事件,为第二次点击设置另一个事件

  24. 24

    sed替换第一次出现参数的所有内容,而第二次出现的内容则不替换任何内容

  25. 25

    FPDF第二页与第一页不匹配

  26. 26

    映射并发送第一页到第二页中的数据列表

  27. 27

    如果创建了第二页,则在第一页上隐藏页脚

  28. 28

    第一次保存到表但第二次不保存会导致错误:sqlalchemy中的NOT NULL约束失败

  29. 29

    当第一页与第二页具有相同的字段时,等待第二页加载水豚的正确方法

热门标签

归档