颤抖地将json数据解码为对象列表

时间:2020-07-25 22:42:53

标签: json flutter

我正在尝试将json数据解码为对象列表,以便稍后与provider一起使用,我没有收到任何错误,但列表仍然为空。 这是我知道的代码:

class Mobile {
final String name;
final String camera;
final String cpu;
final String price;
final String memory;
final String battery;
Mobile({this.name,this.camera,this.cpu,this.memory,this.battery,this.price});
}

     class MobilesData extends ChangeNotifier {
     List<Mobile> _mobiles = [
     ];

 Future<void> fetchmob() async{
const url='http://10.0.2.2/mobitech/index.php';
try{
  final response=await http.get(url);
  final exctractedData=jsonDecode(response.body) as List;
  final List<Mobile> loadedMob=[];
  exctractedData.forEach((mobData){
    loadedMob.add(Mobile(
      name: mobData['name'],
      camera: mobData['system'],
      memory: mobData['cpu'],
      cpu: mobData['gpu'],
      battery: mobData['battery'],
      price: mobData['price_eg'],
    ));
  });
  print("exctractedData:$exctractedData");
  print("loadedMob:$loadedMob");
  _mobiles=loadedMob;
  print("_mobiles:$_mobiles");
  notifyListeners();
}catch(error){
  throw error;
}
 }
List<Mobile> get mobiles{
return [..._mobiles];
 }
}

get mobiles方法,exctractedData列表,loadedMob列表,mobiles列表的输出:

I/flutter (28994): Instance of 'Future<void>'
I/flutter (28994): get mobiles method output:[]
I/flutter (28994): exctractedData:[{mob_id: 1, name: Find x50, screen: 6.5 
inches PLS TFT capacitive touchscreen, 16M colors., screen_protect: Gorilla 
glass 3., screen_res: 720 x 1600 pixels, 20:9 ratio (~270 ppi density)., 
system: Android 10, One UI 2.0., cpu: Octa-core (4x2.0 GHz Cortex-A55 & 4x2.0 
GHz Cortex-A55)., num_core: Octa-core., gpu: Mali-G52., memory: 32/64 GB. , 
ram: 3/4/6 GB., battery: 5000 mAh., camera_main: 48 MP., camera_feature: LED 
flash, panorama, HDR., camera_video: 1080p@30fps., camera_ultra: 8 MP, f/2.2, 
123˚, 1/4.0", 1.12µm., camera_tele: none, camera_depth: 2 MP, f/2.4., 
camera_micro: 2 MP, f/2.4., camera_self: 13 MP, f/2.2, (wide), 1/3.1", 
1.12µm, camera_self_feature: LED flash., camera_self_video: 1080p@30fps, 
price_eg: unavailable, price_sa: unavailable, price_uae: unavailable, 
price_jo: unavailable, price_sy: 220000 S.P, price_alg: unavailable, mob_cat: 
2}, {mob_id: 2, name: Galaxy A71, screen: 6.7 inches Super AMOLED Plus 
capacitive touchscreen, 16M colors., screen_protect: Corning Gorilla Glas
I/flutter (28994): loadedMob:[Instance of 'Mobile', Instance of 'Mobile']
I/flutter (28994): _mobiles:[Instance of 'Mobile', Instance of 'Mobile']

0 个答案:

没有答案