dart 如何在一个函数中调用两个API

31moq8wy  于 2023-04-09  发布在  其他
关注(0)|答案(1)|浏览(134)

我想从数据库中获取数据。我有2个API 1:以获取记录信息,如姓名、地址、公司等。另一个API 2:获取ProfilePhoto。我从第一个API获取输出,但无法获取profilePhoto Api。那么我如何在一个函数中调用两个api?函数是

Future<DataDetails> getData1() async {
    var id = 34;

    final response1 = await http.get(Uri.parse("Api Url"));

    if(response1.statusCode == 200) {

      var temp = DataDetails.fromJson(jsonDecode(response1.body));
      print(temp);
      return temp;
    }

    return DataDetails.fromJson(jsonDecode(response1.body)) ;
}

FutureBuilder(
                 future: getData(),
                  builder: (context, snapshot) {
                    return Screenshot(
                              controller: _ssController,
                              child: GestureDetector(
                                onTap: () {
                                  Navigator.push(context, MaterialPageRoute(builder: (context) => OntapView()));
                                },
                                child: Stack(
                                  children: [
                                    Container(
                                      padding: const EdgeInsets.all(10),
                                      width: 350,
                                      decoration: BoxDecoration(
                                          borderRadius: BorderRadius.circular(15),
                                          // color: Colors.amber[100]
                                          color: const Color(0xffccf7ff)
                                      ),
                                      child:  Column(
                                        children: [
                                          Container(
                                            padding: EdgeInsets.all(10),
                                            child: Row(
                                              children: [
                                                Container(
                                                  height: 15,
                                                  width: 200,
                                                  child: Text('${snapshot.data!.cardName} ',
                                                    style: TextStyle(fontWeight: FontWeight.bold),),
                                                ),
                                     SizedBox(height: 8),
                                          Row(
                                            crossAxisAlignment: CrossAxisAlignment.start,
                                            mainAxisAlignment: MainAxisAlignment.start,
                                            children: [
                                              CircleAvatar(
                                                radius: 45,
                                                backgroundColor: Colors.grey,
                                                backgroundImage: FileImage(File('${snapshot.data!.profilePhotoPath}')),        
                                              ),
                                              const SizedBox(width: 10),
                                              Container(
                                                  padding: const EdgeInsets.only(left: 20,top: 10),
                                                  width: 130,
                                                  height: 100,
                                                  child: Column(
                                                    mainAxisAlignment: MainAxisAlignment.start,
                                                    children: [
                                                      Text('${snapshot.data!.name} ',
                                                        style: const TextStyle(fontSize: 22,fontWeight: FontWeight.bold),),
                                                      const SizedBox(height: 5),
                                                      Container(
                                                        child: Text('${snapshot.data!.address}',
                                                          style: const TextStyle(fontSize: 12),),
                                                      )
                                                    ],
                                                  )
                                              ),

我是新的Flutter。如果可能的话,请分享示例代码。

r1wp621o

r1wp621o1#

我不确定我是否理解正确,但如果你需要调用另一个API(我猜是“ProfilePhoto”),你不应该在调用该API之前放置任何“return”,因为“return temp”和“return DataDetails.fromJson(jsonDecode(response1.body));“将停止函数的后续操作。如果对API的第一次调用是行:

final response1 = await http.get(Uri.parse("Api Url"));

这意味着你也应该返回这个响应。
它看起来应该是这样的:

Future<DataDetails> getData1() async {
var id = 34;

// Call first api ?
final response1 = await http.get(Uri.parse("Api Url"));

if(response1.statusCode == 200) {
// Call second API ? 
  var temp = DataDetails.fromJson(jsonDecode(response1.body));
  print(temp);
// return both of variable
  return [temp, response1];
} else {
return error;
}

你也可以打印一下response 1,看看它是否是200

相关问题