Flutter Child State 未根据更改的父状态更新:FlutterFirestore Stream

Posted

技术标签:

【中文标题】Flutter Child State 未根据更改的父状态更新:FlutterFirestore Stream【英文标题】:Flutter Child State not updating based on changing Parent State: FlutterFirestore Stream 【发布时间】:2021-09-25 15:56:56 【问题描述】:

我有两个小部件:HomeEntryList。在Home 小部件中,我可以更改始终传递给EntryList 的值date,然后应该将其更新为Stream 小部件。

但是,当我更改 Date 值时,EntryList 小部件仍然显示初始日期的项目,而不是这个新日期

Home

class HomePage extends StatefulWidget 
  const HomePage();
  @override
  _HomePageState createState() => _HomePageState();


class _HomePageState extends State<HomePage> 
  DateTime date = new DateTime.now();
  void changeDate(int num) 
    var newDate =
        new DateTime(this.date.year, this.date.month, this.date.day + num);
    setState(() 
      date = newDate;
    );
  
   @override
   Widget build(BuildContext context) 
   
    return Scaffold(
        body: Column(
         children: [              
                // Change Date and display Date here
                Row(
                  children: [
                       ElevatedButton(
                          onPressed: () 
                            changeDate(-1);
                       ),
                      Text(date.toString()),
                      ElevatedButton(
                          onPressed: () 
                            changeDate(1);
                       ),
                        
                   ]
                ),
                // Our main child component EntryList and passing in date
                Row(children: [EntryList(this.date)]),
         ]
       )
    )
   

EntryList

class EntryList extends StatefulWidget 
  final DateTime date;
  const EntryList(this.date);
  @override
  _EntryListState createState() => _EntryListState();


class _EntryListState extends State<EntriesList> 
  Stream<QuerySnapshot> _entryStream; // my stream

  void getEntries(snapshot) 
     // code to get items - this works just fine
  

  @override
  // This is where we use the props in this widget. Not updating after changing parent date value
  void initState() 
    _entryStream = FirebaseFirestore.instance
        .collection(widget.date)
        .snapshots();
    super.initState();
   
   Widget build(BuildContext context) 
       return (StreamBuilder(
       stream: _entryStream,
       builder: (BuildContext context, AsyncSnapshot snapshot) 
        if (snapshot.hasError) 
          return Text("Something went wrong");
        

        if (snapshot.connectionState == ConnectionState.waiting) 
          return CircularProgressIndicator();
        
        return new ListView(
          children: getEntries(snapshot),
        );,
      ,
    ));
  
       
   


谁能告诉我为什么不更新?我认为这可能与使用 void dispose 有关,但我不确定如何在这种情况下使用它。

相关(但不是我的解决方案)链接: How to Set/Update State of StatefulWidget from other StatefulWidget in Flutter?

【问题讨论】:

【参考方案1】:

我发现了一个名为 didUpdateWidget 的函数,它允许我在父值更改时更新我的​​小部件。

这是对我有帮助的答案:Flutter: re-render child widget on parent state change

  @override
  void didUpdateWidget(old) 
    super.didUpdateWidget(old);
    String dateString =
        '$widget.date.month-$widget.date.day-$widget.date.year';
    _entryStream = FirebaseFirestore.instance
        .collection('users')
        .doc(FirebaseAuth.instance.currentUser.uid)
        .collection(dateString)
        .snapshots();
  

现在,一旦日期更改,我的子小部件会正确显示新数据。

【讨论】:

这个答案是救命稻草

以上是关于Flutter Child State 未根据更改的父状态更新:FlutterFirestore Stream的主要内容,如果未能解决你的问题,请参考以下文章

错误:flutter/lib/ui/ui_dart_state.cc(148) 未处理的异常

Flutter pod 安装问题 - #<Dir:0x00007fa6f7e2ec80> 的未定义方法 `each_child'

Flutter [错误:flutter/lib/ui/ui_dart_state.cc(177)] 未处理的异常:类型“int”不是类型转换中“String”类型的子类型

错误:flutter/lib/ui/ui_dart_state.cc(177) 未处理的异常:NoSuchMethodError:方法“插入”在 null 上调用

Flutter:如何根据可用空间裁剪文本?

如何解决此问题 [错误:flutter/lib/ui/ui_dart_state.cc(199)] 未处理的异常:NoSuchMethodError:getter 'phone' 被调用为 null。