扑扑中的嵌套未来 [英] Nested Future in Flutter

查看:70
本文介绍了扑扑中的嵌套未来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Flutter的新手(来自网络,尤其是JS/VueJS)

I'm new to Flutter, (comming from web and especially JS/VueJS)

我在firebase中有一个数据库,数据库中有一个叫做edito的集合,我有另一位具有特定ID的艺术家来用它来调用Deezer Api.

I'm have a db in firebase that has a collection called edito and inside, i have different artist with a specific Id to call Deezer Api with it.

所以我要做的是首先调用我的数据库,并获取每个艺术家的ID,然后将该ID放入函数中作为参数以完成url.

So what i want to do is first called my db and get the Id for each of artist and then put this id in a function as parameter to complete the url.

我做了2个Future函数,一个调用db,一个调用api.

I did 2 Future function, one to call the db and one to call the api.

但是我不明白如何在构建中与其他人一起使用以获取包含每个数据的deezer api信息的列表视图.

But i don't understand how to use one with the others in the build to get a listview with the information of the api of deezer for each data.

我正在获取列表,但它陷入了无休止的循环.

i'm getting the list but it's stuck in and endless loop.

我所有的应用程序都将在此嵌套函数上,是否可以执行此操作并在我想要的任何小部件中调用它?

All of my app will be on this nested function, is it possible to do this and call it in any widget that i want ?

这是我的代码,谢谢

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';


class GetAlbum extends StatefulWidget {
  @override
  _GetAlbumState createState() => _GetAlbumState();
}

class _GetAlbumState extends State<GetAlbum> {
  
  Map mapResponse;

  Future<QuerySnapshot> getDocument() async{
    return FirebaseFirestore.instance.collection("edito").get();
  }


  Future<dynamic> fetchData(id) async{
    http.Response response;
    response = await http.get('https://api.deezer.com/album/' + id);
    if(response.statusCode == 200){
      setState(() {
        mapResponse = json.decode(response.body);
      });
    }
}

  Future<dynamic> getDocut;
  Future<dynamic> getArtist;

@override
  void initState() {
   getDocut = getDocument();
   getArtist = fetchData(null);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<QuerySnapshot>(
      future : getDocut,
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
        if(!snapshot.hasData) {
          return CircularProgressIndicator();
        }else{
          return new ListView(
              children: snapshot.data.docs.map<Widget>((document){
                print(document.data().length);
                  return FutureBuilder(
                      future: fetchData(document.data()['idDeezer'].toString()),
                      builder: (context, snapshot){
                        return Container(
                          child: mapResponse==null?Container(): Text(mapResponse['title'].toString(), style: TextStyle(fontSize: 30),),
                        );
                      }
                  );
          }).toList(),
          );
        }

      },
    );
  }
}



推荐答案

我尝试在第一个中添加firebase,但是我在get AllSlowDAta中获得的id为空,但是我对Future.delayed的理解是正确的.>

I tried to add firebase in the first one but i get null for the id in the get AllSlowDAta but i got it right with the Future.delayed.

  // linked async calls
  Future<String> getAllSlowData() async {
    String id = await loadId(); // make 1st async call for id
    return loadMoreData(id: id); // use id in 2nd async call
  }

  Future<dynamic> loadId() async {
   //return await Future.delayed(Duration(seconds: 2), () => '302127');
   await FirebaseFirestore.instance.collection("edito")
        .get()
        .then((QuerySnapshot querySnapshot) {
           querySnapshot.docs.forEach((doc) {
           
               return  doc.data()["idDeezer"];
          });
    });
  }

  Future<dynamic> loadMoreData({String id}) async {
    http.Response response;
    response = await http.get('https://api.deezer.com/album/' + id);
    if(response.statusCode == 200){
      setState(() {
       return json.decode(response.body);
      });
    }
  }

这篇关于扑扑中的嵌套未来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆