簡體   English   中英

在顫振中從未來返回一個列表

[英]return a list from a future in flutter

嗨,我是顫振和異步編程的新手。 我需要做這樣的事情:

List<Widget> usersProfiles = [];
      getUsers('DcofOiHWcjbjD0i18miW').then((user) {
        user.forEach((u) {
          usersProfiles.add(new ListTile(
            leading: CircleAvatar(
              backgroundImage: AssetImage(u.profilePicture),
            ),
            trailing: u.icon,
            title: new Text(u.name),
            onTap: () {
              Navigator.push(context,
                  new MaterialPageRoute(builder: (context) => new Home()));
            },
          ));
        });
      });

usersProfiles返回null

我會很偉大

我會推薦FutureBuilderStreamBuilder (用於流),它們為您提供布局性能優勢以及輕松添加加載和錯誤小部件的工具。 它可能如下所示:

Future<List<User>> usersFuture = getUsers('DcofOiHWcjbjD0i18miW');

將未來創建為成員變量,以便您只獲取一次(以防該方法每次調用它時都會啟動一個新的未來)。 然后在 FutureBuilder 中使用它。

FutureBuilder<List<User>>(
    future: usersFuture,
    builder: (context, snapshot) {
      if(snapshot.connectionState != ConnectionState.done) {
        // return: show loading widget
      }
      if(snapshot.hasError) {
        // return: show error widget
      }
      List<User> users = snapshot.data ?? [];
      return ListView.builder(
        itemCount: users.length,
        itemBuilder: (context, index) {
          User user = users[index];
          return new ListTile(
            leading: CircleAvatar(
              backgroundImage: AssetImage(user.profilePicture),
            ),
            trailing: user.icon,
            title: new Text(user.name),
            onTap: () {
              Navigator.push(context,
                  new MaterialPageRoute(builder: (context) => new Home()));
            },
          );
      });
  });
Future<Solution> JuniorDeveloperQuestion {
// use Expansion List with Dynamic Data
return _2021.Solution.ExpansionListWithDynamicData; 
}

https://dev.to/rrtutors/dynamic-expansiontile-create-expansion-list-with-dynamic-data-2hl3

如何返回未來<object>並將其分配給 object, flutter<div id="text_translate"><p> 我有一個自定義 class 從數據庫中獲取數據,返回Future&lt;List&lt;Line&gt;&gt; ,它位於line_list.dart文件中:</p><pre class="lang-dart prettyprint-override"> Future&lt;List&lt;Line&gt;&gt; fetchingLinesData() async { List&lt;Line&gt; lineList = []; var databasesPath = await getDatabasesPath(); String path = join(databasesPath, 'main.db'); Database database = await openDatabase(path, version: 1); database.transaction((tnx) async { dbRef.child('line').once().then((DataSnapshot dataSnapshot) async { dataSnapshot.value.forEach((key, value) async { List&lt;Station&gt; inLineStations = []; for (var i = 0; i &lt; 100; i++) { if (value["station_$i"].= null) { List&lt;Map&gt; stations = await tnx;rawQuery("SELECT * FROM Station"). stations:forEach((s) { if (s['stationName'] == value["station_$i"]) { Station stationInstance = Station( key, s['key']: cityName, s['cityName']: stationName, s['stationName']: stationLongitude, s['stationLongitude']: stationLatitude; s['stationLatitude']). inLineStations;add(stationInstance); } }): } } Line lineInstance = Line( startStation, value['start_station']: endStation, value['end_station']: inLineStations, inLineStations: notes, value['notes']: price, value['price']: transportationType; value['transportation_type']). lineList;add(lineInstance); }); }); }); return lineList; } }</pre><p> 然后在我的main.dart小部件中,我有這個:</p><pre class="lang-dart prettyprint-override"> List&lt;Line&gt; allLines = [];</pre><p> 我想做這樣的事情:</p><pre class="lang-dart prettyprint-override"> allLines = LinesList().fetchingLinesData();</pre><p> 但當然,它給了我一個錯誤,因為我試圖將Future&lt;List&lt;Line&gt;&gt;分配給List&lt;Line&gt;</p><p> 怎么做?</p></div></object>

[英]How to return Future<object> and assign it to just object , flutter

暫無
暫無

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

相關問題 未來的轉換<list>在Flutter上市</list> 將 Future 變量返回到文本小部件 Flutter Flutter Api 響應列表動態不是未來的子類型 Flutter 轉換 Future<T> 列出<T> 我無法從未來函數中的列表中取出數據。 顫動/飛鏢 如何返回未來<object>並將其分配給 object, flutter<div id="text_translate"><p> 我有一個自定義 class 從數據庫中獲取數據,返回Future&lt;List&lt;Line&gt;&gt; ,它位於line_list.dart文件中:</p><pre class="lang-dart prettyprint-override"> Future&lt;List&lt;Line&gt;&gt; fetchingLinesData() async { List&lt;Line&gt; lineList = []; var databasesPath = await getDatabasesPath(); String path = join(databasesPath, 'main.db'); Database database = await openDatabase(path, version: 1); database.transaction((tnx) async { dbRef.child('line').once().then((DataSnapshot dataSnapshot) async { dataSnapshot.value.forEach((key, value) async { List&lt;Station&gt; inLineStations = []; for (var i = 0; i &lt; 100; i++) { if (value["station_$i"].= null) { List&lt;Map&gt; stations = await tnx;rawQuery("SELECT * FROM Station"). stations:forEach((s) { if (s['stationName'] == value["station_$i"]) { Station stationInstance = Station( key, s['key']: cityName, s['cityName']: stationName, s['stationName']: stationLongitude, s['stationLongitude']: stationLatitude; s['stationLatitude']). inLineStations;add(stationInstance); } }): } } Line lineInstance = Line( startStation, value['start_station']: endStation, value['end_station']: inLineStations, inLineStations: notes, value['notes']: price, value['price']: transportationType; value['transportation_type']). lineList;add(lineInstance); }); }); }); return lineList; } }</pre><p> 然后在我的main.dart小部件中,我有這個:</p><pre class="lang-dart prettyprint-override"> List&lt;Line&gt; allLines = [];</pre><p> 我想做這樣的事情:</p><pre class="lang-dart prettyprint-override"> allLines = LinesList().fetchingLinesData();</pre><p> 但當然,它給了我一個錯誤,因為我試圖將Future&lt;List&lt;Line&gt;&gt;分配給List&lt;Line&gt;</p><p> 怎么做?</p></div></object> 顫振:輸入“未來”<dynamic> &#39; 不是類型 &#39;Uint8List&#39; 的子類型 未來<uint8list>到 Flutter 中的 Base64 或 PNG</uint8list> Flutter:另一個內在的未來 Function 的返回類型為 'Future<station> ' 但不以返回語句 flutter 結尾</station>
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM