【问题标题】:how to avoid crash when data is missing and throw exeption in flutter如何在数据丢失时避免崩溃并在颤动中抛出异常
【发布时间】:2022-10-01 07:37:08
【问题描述】:

这是我从网络捕获数据的代码:

它每次都会因多种异常而崩溃,我想在屏幕上显示错误但不要使应用程序崩溃并终止它

Future<SCoin> fetchCoinData(int giveMeIndex) async {
  final response = await http.get(Uri.parse(url));
  final jsonresponse = json.decode(response.body);

if (response.statusCode == 200) {
for (var i in jsonresponse) {
  var coinItem = SCoin(
      name: i[\'name\'],
      image: i[\'image\'],
      current_price: i[\'current_price\']);
  coins.add(coinItem);
}
return SCoin.fromJson(jsonresponse[giveMeIndex]);
} else {
throw Exception(response.statusCode);
 }
}

这是我的小部件来显示数据:

FutureBuilder<SCoin>(
                                                future: fetchCoinData(2),
                                                builder:
                                                    (context, snapshot) {
                                                  if (snapshot.hasData) {
                                                    return Column(
                                                      children: [
                                                        Container(
                                                          width: 45,
                                                          height: 45,
                                                          child: Image.network(
                                                              snapshot.data!
                                                                  .coinImage),
                                                        ),
                                                        Text(snapshot
                                                            .data!.name),
                                                        Text(snapshot.data!
                                                            .current_price
                                                            .toString())
                                                      ],
                                                    );
                                                  } else if (snapshot
                                                      .hasError) {
                                                    return Text(
                                                        \'${snapshot.error}\');
                                                  }

                                                  // By default, show a loading spinner.
                                                  return const CircularProgressIndicator();
                                                }),

    标签: flutter error-handling


    【解决方案1】:

    这是通过trycatch 尝试运行代码来完成的,如果有任何错误,您可以评估并捕获它,并将未来设置为FutureBuilder 应该包装在另一个尝试它的函数中,然后捕获错误,例如:

    Future tryCatchMethod(int number) async {
    
    try {
    await fetchCoinData(number);
    } on Exception catch(error) {
    // Here throw you exceptions and take actions based on your needs
    }}
    

    并将该新方法分配给FutureBuilder

    future: tryCatchMethod;
    

    这将尝试运行您的 Future 方法,如果成功,则通常不会发生任何事情,但如果发生错误并且抛出错误,它将执行 catch 块代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-30
      • 1970-01-01
      • 2020-09-27
      • 2021-01-13
      • 1970-01-01
      • 2013-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多