- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有通用类来请求名为 的服务器可重用请求服务器 ,如果在向服务器发出请求时发生错误,该类将处理一些异常。
可重用的请求服务器
class ReusableRequestServer<T> {
Future<T> requestServer(FutureOr<T> requestServer()) async {
try {
return await requestServer();
} on FormatException catch (_) {
throw ConstText.FORMAT_EXCEPTION;
} on TimeoutException catch (_) {
throw ConstText.TIMEOUT_EXCEPTION;
} on SocketException catch (_) {
throw ConstText.NO_CONNECTION;
} catch (e) {
throw e.toString();
}
}
}
final reusableRequestServer = ReusableRequestServer();
get
请求从服务器获取移动版本,我也实现了
可重用请求服务器 也。
class MobileVersionApi {
Future<List<MobileVersionModel>> getNewestMobileVersion() async {
var result = await reusableRequestServer.requestServer(() async {
final response = await http
.get('${appConfig.baseApiUrl}/${appConfig.mobileVersionController}/getNewestVersion')
.timeout(Duration(seconds: 10));
final Map<String, dynamic> responseJson = json.decode(response.body);
if (responseJson["status"] == "ok") {
List mobileVersionList = responseJson["data"];
List<MobileVersionModel> result =
mobileVersionList.map((json) => MobileVersionModel.fromjson(json)).toList();
return result;
} else {
throw responseJson['message'];
}
});
return result;
}
}
final mobileVersionApi = MobileVersionApi();
Future<List<MobileVersionModel>> mobileVersion;
Future<List<AppInfoModel>> appInfo;
@override
void initState() {
super.initState();
final appInfoProvider = Provider.of<AppInfoProvider>(context, listen: false);
mobileVersion = getMobileVersion();
appInfo = getAppInfo(appInfoProvider);
}
Future<List<MobileVersionModel>> getMobileVersion() async {
try {
final result = await mobileVersionApi.getNewestMobileVersion();
return result;
} catch (e) {
globalF.showToast(message: e, isError: true);
return null;
}
}
Future<List<AppInfoModel>> getAppInfo(AppInfoProvider appInfoProvider) async {
try {
final result = appInfoApi.getLogoClient().then((value) {
appInfoProvider.setAppInfo(value);
if (value[0].updateTime != appInfoProvider.appInfo.updateTime) {
globalF.clearCacheApp(); //clears all data in cache.
}
});
return result;
} catch (e) {
globalF.showToast(message: e, isError: true);
return null;
}
}
Widget build(BuildContext context) {
return Scaffold(
body: Consumer2<GlobalProvider, UserProvider>(
builder: (_, globalProvider, userProvider, __) => FutureBuilder(
future: Future.wait([
mobileVersion,
appInfo,
]),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return LoadingFutureBuilder();
} else {
if (snapshot.hasError) {
return ErrorFutureBuilder(errorText: snapshot.error.toString());
} else {
return Text('Hore');
}
}
},
),
),
);
}
Exception has occurred.
SocketException (SocketException: Failed host lookup: 'xxx.net' (OS Error: No address associated with hostname, errno = 7))
最佳答案
在 catch 语句中尝试返回 Future.error。
前任:
catch (e){
return Future.error(e.toString());
}
关于 flutter : Futurebuilder not catch exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61440363/
假设我从不同的 Url 获取不同的列表所以 Future 函数看起来像这样 Future> getCityDetails(Region selectedRegion) async {} Future>
我正在为用户列表使用 FuturBuilder。我通过 futur: fetchpost() 通过 API 获取用户。在专栏的开头,我实现了一个搜索栏。那么我该如何实现我的搜索栏正在搜索呢? Cont
我必须为屏幕的不同类型的类别构建多个 future builder ,例如:每周交易、全部、新到货等。我现在的代码非常准系统,但在这里。 +主屏幕 import 'package:flutter/ma
当我向下滚动时接近屏幕末尾时,我正在尝试更新我的 Thread 对象列表(以在我继续向下滚动时显示无限的项目列表) 我目前的设置如下: import 'package:flutter/material
用例: FutureBuilder 加载数据并构建小部件* 我按下一个按钮 → 我改变页面/屏幕 我按一个按钮回到 futureBuilder 的屏幕 问题:futureBuilder 是否重新加载数
文章Fetch data from the internet显示了 FutureBuilder 的以下代码片段处理快照。 FutureBuilder( future: post, builde
我的futurebuilder 在屏幕上显示错误几秒钟,然后显示结果这是错误日志: The following NoSuchMethodError was thrown building Future
我正在使用Future构建器从快照中获取数据并将其过滤到另一个创建标记的Future函数中。我通过过滤所有餐厅的位置获得了结果,但是该功能会不断更新,并且数据只会闪烁。 编辑2:JSON数据
当应用程序使用 Future 初始化时我从远程 JSON 获取数据文件。 而不是 return -在 widget当FutureBuilder有数据,我想导航到新屏幕。 我现在使用的方式: @over
我无法让FutureBuilder显示我的计算中的任何数据。我知道计算绝对正确,因为我可以得到正确的答案以打印到控制台中,但似乎无法使其显示在应用程序本身中。 这是代码的代码: Future get
我想显示一个在按下按钮时调用 api 检索到的对象。 我想在按下 floatingActionButton 时更新 FutureBuilder 小部件: class PostScreen ex
在我作为新屏幕的简单代码中,不幸的是 FutureBuilder 工作并从方法中获取数据两次!! 我不确定是什么问题,我该如何避免 class LessonDetail extends Statefu
我想显示一个在按下按钮时调用 api 检索到的对象。 我想在按下 floatingActionButton 时更新 FutureBuilder 小部件: class PostScreen ex
在我作为新屏幕的简单代码中,不幸的是 FutureBuilder 工作并从方法中获取数据两次!! 我不确定是什么问题,我该如何避免 class LessonDetail extends Statefu
我正在尝试查找离我当前位置最近的人。所以我正在从 firestore 中获取所有文档。 我的firestore结构是这样的, 所以我正在获取所有用户并通过计算距离来检查最近的用户,我想将过滤后的数据传
我试图在加载 Future Builder 中的数据时显示循环进度指示器,我尝试了两种方法来添加它,但是它们都不起作用。我怎样才能达到预期的结果? 我的代码 class _MyHomePageStat
当我更改特定 futurebuilder 小部件所依赖的状态时,在异步任务完成之前不会重新绘制小部件。我希望在状态改变后立即重绘小部件。下面是 futurebuilder 的代码: child: ne
我正在从这里获取 json:https://api.myjson.com/bins/1g3xpe ,在我的 flutter 应用程序中。 这是代码(我已经删除了不相关的部分)。问题是,FutureBu
我想知道什么时候应该使用 future builder。例如,如果我想发出一个 http 请求并在 ListView 中显示结果,一旦你打开 View ,我应该使用 future builder 还是
我想测试一个具有 Future 构建器的函数。函数是: Widget loadWidget() { return new FutureBuilder(
我是一名优秀的程序员,十分优秀!