English 中文(简体)
挥霍:我如何能够在未来的职能上增加网络检查和显示
原标题:Flutter: How can I add network check and showDialog on a Future function
  • 时间:2023-08-10 17:29:48
  •  标签:
  • flutter

我的乌特产阶级采用的方法如下: 一个用于检查网络连接和显示进展方言。

static Future<bool> isInternet() async
  {
    bool result = await InternetConnectionChecker().hasConnection;
    return result;
  }

  static ShowProgress(BuildContext context) {
    return showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return Center(
            child: SpinKitRotatingCircle(color: const Color(0xff2799fa), size: 60.0, ),
          );
        });
  }

我需要将其应用到下文的APIC电话部分:

class _UserDetailsState extends State<UserDetails> {
  ProfileResponse? profileResponse;
  Future<void> fetchData() async {
    try {
      //Utility.ShowProgress(context);
      final response = await getProfileDetails();
      if (response.statusCode == 200) {
        Map<String, dynamic> parsedData = jsonDecode(response.body);
        setState(() {
          profileResponse = ProfileResponse.fromJson(parsedData);
        });
      } else {
        // setState(() {
        //   _data =  Error: ${response.statusCode} ;
        // });
      }
    } catch (error) {
      // setState(() {
      //   _data =  Error: $error ;
      // });
    }
    //Navigator.of(context).pop();
  }

  @override
  void initState() {
    fetchData();
  }
  
  @override
Widget build(BuildContext context) {
return Container(
     )
}
  
  Future<http.Response> getProfileDetails() async{
    if(await Utility.isInternet()){
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    //reading values from SharedPreferences and calling the API
    final String url = My API Path;
    final response = await http.get(Uri.parse(url));
    return response;
    }
    else{
      //show no network alert
    }
}

我在<代码>fetchData()中添加了“ProgressDialog,如该守则中评论的代码,但该编码在《国际投资指南》中并未显示使用该软件。

此外,我还增加了关于<代码>植被详细数据()的网络核对,但显示的是以下错误。

The body might complete normally, causing null to be returned, but the return type, FutureOr , is a potentially non-nullable type. Try adding either a return or a throw statement at the end.

我如何解决这两个问题?

最佳回答

每当你确定某种预期产出的方法时,你的代码应退还所定义的相同类型或错误。

在你们的法典中,你宣布了这样的未来:

 Future<http.Response> getProfileDetails() async{

You should always return a Future<http.Response>. However, when the first If condition is not True, there is nothing to be returned, resulting in your error.

问题回答

固定代码,有进步的方言和网络核对。

Future<http.Response?> getProfileDetails() async
{
    if(await Utility.isInternet())
    {
      Utility.ShowProgress(context);
      //reading values from SharedPreferences and calling the API
      final String url = My API Path;
      final response = await http.get(Uri.parse(url));
      Navigator.of(context).pop();
      return response;
    }
    else
    {
      Utility.showToastMessage("No internet connection!",Toast.LENGTH_SHORT,
          ToastGravity.CENTER, Colors.white, 16.0 );
      return null;
    }
}




相关问题
Flutter App cannot be installed on Android TV

I m building a Flutter app that should support Android TV and Mobile devices. Despite Google Play shows that it is supported, I cannot install app on my MiBox device. While trying to install it, both ...

Moving the icon button to the right causes rendering issues

I am trying to align the icon button to the right of the Text field, I tried the row which made the icon button not centered vertically and now I am trying out the Stack widget. Here is my code - ...

热门标签