我正在尝试在 Flutter 中创建一个下拉按钮。我从我的数据库中获取一个列表,然后我将列表传递给我的dropdownButton 所有工作,数据按预期显示,但是当我从中选择一个元素时,我收到此错误:
There should be exactly one item with [DropdownButton]'s value: Instance of 'Tag'.
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 805 pos 15: 'items == null || items.isEmpty || value == null ||
items.where((DropdownMenuItem<T> item) {
return item.value == value;
}).length == 1'
Run Code Online (Sandbox Code Playgroud)
我尝试将DropdownButton 值设置为 null它可以工作,但随后我看不到所选元素。
这是我的代码:
FutureBuilder<List<Tag>>(
future: _tagDatabaseHelper.getTagList(),
builder: …Run Code Online (Sandbox Code Playgroud) dart drop-down-menu flutter dropdownbutton flutter-futurebuilder
我收到以下错误:
A value of type 'Future<int>' can't be assigned to a variable of type 'int'
Run Code Online (Sandbox Code Playgroud)
它可能是另一种类型而不是int,但基本上模式是
A value of type 'Future<T>' can't be assigned to a variable of type 'T'
Run Code Online (Sandbox Code Playgroud)
所以...
Future?Future<T>?我正在使用FutureBuilder来显示从服务器加载的数据。我只想在应用程序启动时显示一次加载状态,这就是我从 initState 调用 API 的原因。我从服务器获取的数据可能会发生变化,为了反映 UI 中的变化,我使用了refreshIndicator。问题是我无法想出更新状态的解决方案。
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
GlobalKey<RefreshIndicatorState>();
Future<List<Photo>> _photosServer;
@override
void initState() {
super.initState();
_photosServer = ApiRest.getPhotos();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: () {
_refreshIndicatorKey.currentState.show();
await getPhotosFromServer();
...
},
child: FutureBuilder(
future: _photosServer,
builder: (BuildContext context, snapshot) {
if (snapshot.data == null) { …Run Code Online (Sandbox Code Playgroud) 我在我的项目中使用 Getx 控制器。我已经为 FutureBuilder 创建了用于显示列表的控制器,但未在 Future Function 上设置 .Obs。我正在分享代码。
class PPHomeController extends GetxController {
Future<List<PPProductRenterModel>> listNearProduct;
// i want to set .Obs end of the "listNearProduct" but it's not working because of Future.
FetchNearProductList({@required int price}) async {
listNearProduct = CallGetNearProducts();// Http API Result
}
}
{
PPHomeController _homeController = Get.put(PPHomeController());
Widget mainProductListView() {
return FutureBuilder<List<PPProductRenterModel>>
(builder: (context, AsyncSnapshot<List<PPProductRenterModel>> projectSnap){
if(!projectSnap.hasData){
if(projectSnap.connectionState == ConnectionState.waiting){
return Container(
child: Loading(),
);
}
}
return ListView.builder(
itemCount: projectSnap.data.length,
itemBuilder: (context, index) {
PPProductRenterModel …Run Code Online (Sandbox Code Playgroud) 现在我使用 Flutter 在应用程序中工作,我有 4 种不同的语言,我使用 json (arb 文件)进行本地化(翻译)
我需要传递不同的字符串值,应用程序使用 API 获取它们,如下面的示例所示
AppLocalizations.of(context)!snapshot.data![index].state_pickup[0]
Run Code Online (Sandbox Code Playgroud)
但是,"AppLocalizations.of(context)!"它不会从中获取返回数据, snapshot.data![index].state_pickup[0]而是将其作为字符串查找,并尝试在 AppLocalization.dart 类中搜索匹配的字符串名称?
知道如何将动态字符串参数传递给吗AppLocalizations.of(context)!?
snapshot fetch-api flutter flutter-futurebuilder applocalizations
有什么方法可以在这种情况下使用未来的布尔值,或者有更好的方法吗?
Widget _buildRow(String pair) {
final Future<bool> alreadySaved = DBHelper.getAllEmployees().then((value) => value.contains(pair));
print("Already saved $alreadySaved");
print(pair);
return FutureBuilder(
future: DBHelper.getAllEmployees(),
builder: (context, AsyncSnapshot<List<FavrtTableModel>> snapshot) =>
snapshot.connectionState == ConnectionState.waiting ? Center(
child: Icon(Icons.more_horiz),
):IconButton(
icon: new Icon(
alreadySaved ? Icons.favorite : Icons.favorite_border,
color:alreadySaved? Colors.red : Colors.white,
),onPressed: (){
setState(() {
if (alreadySaved) {
_saved.remove(pair);
_deleteEmployee(pair);
} else {
_saved.add(pair);
_insert(pair);
}
});
},
),
);
}
Run Code Online (Sandbox Code Playgroud) 为了检查互联网连接,我正在使用 data_connection_checker 包,但似乎我不知道如何使用它。我正在使用以下代码通过调用 initstate 来检查互联网连接
Widget _showDialog(String err,String content ,BuildContext context) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: new Text(err),
content: new Text(content),
actions: <Widget>[
new FlatButton(
onPressed: () {
Navigator.of(context).pop();
setState(() {
});
},
child: new Text("Try Again"))
]
);
}
);
}
Future checkConnection(BuildContext context) async{
listener = DataConnectionChecker().onStatusChange.listen((status) {
switch (status){
case DataConnectionStatus.connected:
return "DataConnectionState.connected";
break;
case DataConnectionStatus.disconnected:
InternetStatus = "You are disconnected to the Internet. ";
contentmessage = "Please check your internet …Run Code Online (Sandbox Code Playgroud) 我觉得linter规则discarded_future有问题
有没有办法使用discarded_future lint 规则,而不必每次使用FutureBuilder 时都忽略该规则。使用简单的 FutureBuilder 时触发规则是否正常?如果是,正确的方法是什么?
FutureBuilder<void>(
future: retrieveLostData(),
builder: (
BuildContext context,
AsyncSnapshot<void> snapshot,
) {
switch (snapshot.connectionState) {
case ConnectionState.none:
return buildRowIconPhoto();
case ConnectionState.waiting:
return buildRowIconPhoto();
case ConnectionState.done:
return buildRowIconPhoto();
default:
return buildRowIconPhoto();
}
},
)
Run Code Online (Sandbox Code Playgroud)
Future<void> retrieveLostData() async {
LostDataResponse response = await _imagePicker.retrieveLostData();
if (response.isEmpty) {
return;
}
if (response.file != null && widget.onPhotoTaken != null) {
widget.onPhotoTaken!(
File(response.file!.path),
false,
);
}
}
Run Code Online (Sandbox Code Playgroud) 的_futureData是与用于FutureBuilder从所述检索值后_loadPhobias()功能。
entry_screen.dart
Future _futureData;
final TextEditingController _textEditingController = TextEditingController();
Run Code Online (Sandbox Code Playgroud)
_loadPhobias() 功能似乎没有任何问题。
entry_screen.dart
Future<List<String>> _loadPhobias() async =>
await rootBundle.loadString('assets/phobias.txt').then((phobias) {
List _listOfAllPhobias = [];
List<String> _listOfSortedPhobias = [];
_textEditingController.addListener(() {
...
});
return _listOfSortedPhobias;
});
@override
void initState() {
super.initState();
_futureData = _loadPhobias();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: TextField(
// When the value is changed, the value returned from the _loadPhobias will also change. So I want the FutureBuilder …Run Code Online (Sandbox Code Playgroud) 当目标英雄位于 FutureBuilder 中时,我正在绞尽脑汁寻找一种简单的方法来使英雄动画正常工作。我知道这是行不通的,因为英雄图像必须出现在第二个屏幕的第一帧上,而通过 FutureBuilder 从 Firestore 获取数据时显然不能出现这种情况。
这是我的代码:
第一个屏幕:
Hero(
tag: "postImage",
child: Image.network(post.imageurl),
),
Run Code Online (Sandbox Code Playgroud)
第二个屏幕:
FutureBuilder(
future: _futurePost,
builder: (context, AsyncSnapshot snapshot) {
[...]
return Hero(
tag: "postImage",
child: Image.network(snapshot.data.imageurl),
);
}
)
Run Code Online (Sandbox Code Playgroud)
如何实现这一目标?我很惊讶这不是一个常见问题!
谢谢你!
flutter ×10
dart ×5
future ×2
async-await ×1
connection ×1
fetch-api ×1
flutter-getx ×1
hero ×1
snapshot ×1
sqlite ×1
state ×1