我正在开发一个flutter项目,并且被困在必须从firebase检索数据的地方。 我想获取“产品”的数据,这些是我产品中的字段。
class Product {
final String product_id;
final String title;
final String description;
final double price;
final String image;
final bool isFavorite;
final String id;
final String email;
Product({
@required this.product_id,
@required this.title,
@required this.description,
@required this.image,
@required this.price,
@required this.email,
@required this.id,
this.isFavorite = false,
});
}
这就是我存储数据的方式。
Map<String, dynamic> productdata = {
'title': title,
'description': description,
'image':
'https://upload.wikimedia.org/wikipedvdkfjngjsdnga/a5.jpg',
'price': price,
'id': _authenticatedUser.id,
'email':_authenticatedUser.email,
};
http
.post('https://flutter-blah_blah.firebaseio.com/products.json',
body: json.encode(productdata));
当我运行get命令以检索数据时 得到赞扬:
http.get('https://flutter-blah_blah.firebaseio.com/products.json')
.then((http.Response response) {
print(json.decode(response.body));
我得到上面代码的输出
{-L_x5_aGpDN-bbrqqqqi: {description: kkkkkkk, email: abc@gmail.com, id: fghjdgf, image: https://upload.wikimedia.org/wikipedia/commons/a/a5/Red_Kitten_01.jpg, price: 111.0, title: kitten}, -L_x7ffhrotslg4vS: {description: kkkkkkkk, email: abc@gmail.com, id: fghjdgf, image: https://upload.wikimedia.org/wikipedia/commons/a/a5/Red_Kitten_01.jpg, price: 111.0, title: kitten 2}}
您可以清楚地看到,我的firebase表中已经有2行。 但是之后,我在函数的下一个命令中遇到了问题,即:
final Map<String, Map<String, dynamic>> productListData = json.decode(response.body);
我遇到了一个大错误 错误:
I/flutter ( 6106): _InternalLinkedHashMap<String, dynamic>
E/flutter ( 6106): [ERROR:flutter/shell/common/shell.cc(186)] Dart Error: Unhandled exception:
E/flutter ( 6106): type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Map<String, Map<String, dynamic>>'
E/flutter ( 6106): #0 _MainModel&Model&ConnectedProduct&UserModel&ProductsModel.fetchProducts.<anonymous closure> (package:udemy/scoped_models/connected_product.dart:109:47)
E/flutter ( 6106): #1 _RootZone.runUnary (dart:async/zone.dart:1379:54)
E/flutter ( 6106): #2 _FutureListener.handleValue (dart:async/future_impl.dart:126:18)
E/flutter ( 6106): #3 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:639:45)
E/flutter ( 6106): #4 Future._propagateToListeners (dart:async/future_impl.dart:668:32)
E/flutter ( 6106): #5 Future._complete (dart:async/future_impl.dart:473:7)
E/flutter ( 6106): #6 _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
E/flutter ( 6106): #7 _AsyncAwaitCompleter.complete (dart:async/runtime/libasync_patch.dart:28:18)
E/flutter ( 6106): #8 _completeOnAsyncReturn (dart:async/runtime/libasync_patch.dart:294:13)
E/flutter ( 6106): #9 _withClient (package:http/http.dart)
E/flutter ( 6106): <asynchronous suspension>
E/flutter ( 6106): #10 get (package:http/http.dart:46:5)
E/flutter ( 6106): #11 _MainModel&Model&ConnectedProduct&UserModel&ProductsModel.fetchProducts (package:udemy/scoped_models/connected_product.dart:104:5)
E/flutter ( 6106): #12 _ProductPageState.initState (package:udemy/pages/products.dart:21:18)
E/flutter ( 6106): #13 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3830:58)
E/flutter ( 6106): #14 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
E/flutter ( 6106): #15 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
E/flutter ( 6106): #16 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
E/flutter ( 6106): #17 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
E/flutter ( 6106): #18 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
E/flutter ( 6106): #19 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
E/flutter ( 6106): #20 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
E/flutter ( 6106): #21 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
E/flutter ( 6106): #22 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
E/flutter ( 6106): #23 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
E/flutter ( 6106): #24 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
E/flutter ( 6106): #25 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
E/flutter ( 6106): #26 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
E/flutter ( 6106): #27 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
E/flutter ( 6106): #28 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
E/flutter ( 6106): #29 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
E/flutter ( 6106): #30 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
E/flutter ( 6106): #31 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
E/flutter ( 6106): #32 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
E/flutter ( 6106): #33 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
E/flutter ( 6106): #34 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
E/flutter ( 6106): #35 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4860:14)
E/flutter ( 6106): #36 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
E/flutter ( 6106): #37 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
E/flutter ( 6106): #38 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3732:16)
E/flutter ( 6106): #39 Element.rebuild (package:flutter/src/widgets/framework.dart:3547:5)
E/flutter ( 6106): #40 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3701:5)
E/flutter ( 6106): #41 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3848:11)
E/flutter ( 6106): #42 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3696:5)
E/flutter ( 6106): #43 Element.inflateWidget (package:flutter/src/widgets/framework.dart:2950:14)
E/flutter ( 6106): #44 Element.updateChild (package:flutter/src/widgets/framework.dart:2753:12)
E/flutter ( 6106): #45 ComponentElement.performRebuild (packag
该如何解决? 我需要将解码后的响应存储在Map <\ string,Map <\ string,dynamic >>变量中,使用forEach命令遍历每个元素/行,并将其存储在“ Product”类型的列表中(Product类为并在我的应用页面上显示。 我该怎么办?
答案 0 :(得分:0)
只需更改
final Map<String, Map<String, dynamic>> productListData = json.decode(response.body)
到
final Map<String, Map<String, dynamic>> productListData = json.decode(response.body)?.cast<String, Map<String, dynamic>>();
希望有帮助!