繁体   English   中英

API 响应图像 URL 在颤动中不显示图像

[英]API Response image URL not displaying image in flutter

我正在尝试显示来自 API 响应的图像,但它没有显示任何内容。

我能够显示图像 URL,但不能显示实际图像。

这是我的代码:

API调用:

@override
  void initState() {
    super.initState();
    showProgressBar = true;

    customFun.readToken().then((val) {
      setState(() {
        access_token = val;
      });
      (() async {
       await restApis.getProfile(access_token).then((val) => setState((){
         print(val);
          name = val["data"]["name"];
          _nameController.text = val["data"]["name"];
          _emailController.text = val["data"]["email"];
          _mobileController.text = (val["data"]["contact_number"]).toString();

          profile_image = val["data"]["avatarUrl"];

          print("-----------------------");
          print(profile_image);
          print("-----------------------");
          showProgressBar = false;
        }));
      })();
    });

  }

这是显示图像代码:

(profile_image != '') ?
                           Container(
                             width: 150.0,
                             height: 150.0,
                             decoration: BoxDecoration(
                               shape: BoxShape.circle,
                               border: Border.all(
                                 width: 1.0,
                                 color: Colors.black
                               ),
                               image: DecorationImage(
                                 image: NetworkImage("$profile_image"),
                                 fit: BoxFit.fill
                               ),

                             ),
                           ): Container(),

这是在文本中显示图像 URL,它工作正常:

Container(
                             child: Text("$profile_image")
                           ),

尝试添加setState

customFun.readToken().then((val) {
  setState(() {
    access_token = val;
  });
  (() async {
    await restApis.getProfile(access_token).then((val) => setState((){
      print(val);
      name = val["data"]["name"];
      _nameController.text = val["data"]["name"];
      _emailController.text = val["data"]["email"];
      _mobileController.text = (val["data"]["contact_number"]).toString();

      profile_image = val["data"]["avatarUrl"];

      print("-----------------------");
      print(profile_image);
      print("-----------------------");
      showProgressBar = false;
    }));
    setState((){}); //TODO: ADD THIS
  })();
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM