簡體   English   中英

Flutter:如何在其父內部獲取子Stream Builder的更新?

[英]Flutter: How to get updates of child Stream builder inside its parent?

我有一個Flutter頁面,其中包含一個名為: VisitsHistory的小部件,其中有2個名為VisitsListStatistics的子小部件

子窗口小部件“ VisitsList”具有一個流構建器,該流構建器從Firestore獲取數據並呈現卡列表。

我希望來自streambuilder的數據流不斷更新“統計”小部件。 他們倆共享同一個父母(訪問歷史記錄)。

我嘗試使用Streambuilder的回調通知父對象,然后父對象調用SetState(),但這給了我一個錯誤: setState() or markNeedsBuild called during build

我知道發生錯誤的原因是,我在streambuilder返回應該構建的內容之前進行了回調,但是在其他地方可以放置該回調,以便可以確保構建完成並完成,然后可以安全地調用setState ()在父母中?

注意:我正在使用提供程序作為狀態管理

這是VisitsList小部件/ StreamBuilder的摘錄


    class VisitsList extends StatelessWidget {
      final Function getDebtDataCallback;
      VisitsList(this.getDebtDataCallback);
      @override
      Widget build(BuildContext context) {
        return StreamBuilder<QuerySnapshot>(
          stream: doctorprovider.getVisitsHistory(),
          builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
            if (snapshot.hasError) return new Text('Error: ${snapshot.error}');
            switch (snapshot.connectionState) {
              case ConnectionState.waiting:
                return new CircularProgressIndicator();
              default:
                if (snapshot.data.documents.isEmpty) {
                  return Text(
                    "No Visits yet",
                    style: TextStyle(color: Colors.white),
                  );
                } else {
                  //CALLBACK IS HERE 
                  getDebtDataCallback(snapshot);
                  return new ListView( ......

您可以使用值更新程序將值推入父級嗎? Flutter團隊創建了一個股票示例應用程序來演示此功能。

https://github.com/flutter/flutter/tree/master/examples/stocks

https://flutter.dev/docs/development/data-and-backend/state-mgmt/simple

在孩子中,您需要這個樣板

class StockSettings extends StatefulWidget {
  const StockSettings(this.configuration, this.updater);

  final StockConfiguration configuration;
  final ValueChanged<StockConfiguration> updater;

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

class StockSettingsState extends State<StockSettings> {
void _handleBackupChanged(bool value) {
    sendUpdates(widget.configuration.copyWith(backupMode: value ? BackupMode.enabled : BackupMode.disabled));
}

void sendUpdates(StockConfiguration value) {
    if (widget.updater != null)
      widget.updater(value);
}

在父級中,您傳遞子級的配置更新程序以調用父級的setState

class StocksAppState extends State<StocksApp> {
  StockData stocks;

  StockConfiguration _configuration = StockConfiguration(
    stockMode: StockMode.optimistic,
    backupMode: BackupMode.enabled,
    debugShowGrid: false,
    debugShowSizes: false,
    debugShowBaselines: false,
    debugShowLayers: false,
    debugShowPointers: false,
    debugShowRainbow: false,
    showPerformanceOverlay: false,
    showSemanticsDebugger: false
  );

  @override
  void initState() {
    super.initState();
    stocks = StockData();
  }

  void configurationUpdater(StockConfiguration value) {
    setState(() {
      _configuration = value;
    });
}

routes: <String, WidgetBuilder>{
         '/':         (BuildContext context) => StockHome(stocks, _configuration, configurationUpdater),
         '/settings': (BuildContext context) => StockSettings(_configuration, configurationUpdater)
},

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM