解析JSON文件以获取列表内的列表

时间:2019-04-21 12:33:14

标签: dart flutter nested-lists

我正在工作,它应该从带有对象的Json文件列表中读取,我想在列表内部创建列表,其中第一个列表将给出主题和正文,然后应该转到该特定的另一个列表进行询问并列出每个测量的位置,但是会出现此错误

import 'package:flutter/services.dart' show rootBundle;
import 'dart:io';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:test_app/InspectionDetail.dart';
import 'package:json_annotation/json_annotation.dart';


class InspetionList extends StatefulWidget {
  static String tag = 'inspection-list';
  @override
  _InspetionListState createState() => _InspetionListState();
}

class _InspetionListState extends State<InspetionList> {
  List<Lists> info = [];
  List <Lists> _search =[];
  TextEditingController controller = new TextEditingController();



  Future <Lists>  _getLists() async {
    final data = await http.get(
        "https://api.myjson.com/bins/qiulc");
    List jsonData = json.decode(data.body);

    for (int u =0; u < jsonData.length ; u++ ) {
    //  print(jsonData[u]["subject"]);
      print(jsonData[u]["name"]);
      return (jsonData[u]["subject"]);

    }

  }

  void initState(){
    super.initState();
    fetch();
  }
  Future <List> future;
//Future <String> future ;

  void fetch() async{
    future = _getLists() as Future<List>;

  }

  onSearch(String text) async {
    _search.clear();
    if (text.isEmpty) {
      setState(() {});
      return;
    }
  }
 //  info.forEach((f){
 //    if(f.body.contains(text) || f.subject.contains(text)) _search.add(f);
 //  });
 //  setState(() {

 //  });
 //

  @override
  Widget build(BuildContext context) {
   return new Scaffold(
    appBar: new AppBar(
     title: new Text("List of projects"),
     ),
    body: new Center(
      child: new Column(
       children: <Widget>[
          new FutureBuilder(
              future: _getLists(),
              builder: (BuildContext context, AsyncSnapshot snapshot){
               if(snapshot.hasData){
                 return ListView.builder(
                 itemCount: snapshot.data.length,
                 itemBuilder: (BuildContext context, int index){
                  return
                      ListTile(
                        leading: CircleAvatar(
                          child: Icon(Icons.settings_input_antenna),
                        ),
                        title: Text(snapshot.data[index]["subject"]),
                        subtitle: Text(''),
                      );
                 ListTile(
                   title: Text(snapshot.data[index]["subject"]),
                 );}
                  );
                }else if(snapshot.hasError){
                  return new Text("Error ${snapshot.error}");
                }
              })
        ],
      ),
    )
   );
  }
}




class Lists {
  final String subject;
  final String body;
  Inspection name;

  Lists({
    this.subject,
    this.name,
    this.body
  });

  factory Lists.fromJson(Map parsedJson){
    return Lists(
        subject: parsedJson['subject'],
        body: parsedJson['body'],
        name: Inspection.fromJson(parsedJson['name'])
    );
  }
}
class Inspection{
  final String Lwidth;
  final String Twidth;
  final String height;
  final String circumfernce;
  Inspection({
   this.Lwidth,
   this.Twidth,
   this.circumfernce,
   this.height
 });
 factory Inspection.fromJson(Map<String, dynamic> json){
   return Inspection(
       Lwidth: json['Lwidth'],
       Twidth: json['Twidth'],
       height: json['height'],
       circumfernce: json['circumfernce']
   );
 }}


Error :

[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type 'String' is not a subtype of type 'FutureOr<Lists>'
E/flutter (19254): #0      _InspetionListState._getLists (package:test_app/InspectionList.dart:45:7)
E/flutter (19254): <asynchronous suspension>
E/flutter (19254): #1      _InspetionListState.fetch (package:test_app/InspectionList.dart:59:14)
E/flutter (19254): <asynchronous suspension>
E/flutter (19254): #2      _InspetionListState.initState (package:test_app/InspectionList.dart:53:5)
E/flutter (19254): #3      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3846:58)
E/flutter (19254): #4      ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
E/flutter (19254): #5      Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
E/flutter (19254): #6      Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
E/flutter (19254): #7      SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
E/flutter (19254): #8      Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
E/flutter (19254): #9      Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
E/flutter (19254): #10     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3747:16)
E/flutter (19254): #11     Element.rebuild (package:flutter/src/widgets/framework.dart:3559:5)
E/flutter (19254): #12     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3716:5)
E/flutter (19254): #13     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3711:5)
E/flutter (19254): #14     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
E/flutter (19254): #15     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
E/flutter (19254): #16     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
E/flutter (19254): #17     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
E/flutter (19254): #18     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
E/flutter (19254): #19     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
E/flutter (19254): #20     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
E/flutter (19254): #21     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
E/flutter (19254): #22     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
E/flutter (19254): #23     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)
E/flutter (19254): #24     Element.updateChild (package:flutter/src/widgets/framework.dart:2759:12)
E/flutter (19254): #25     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4876:14)
E/flutter (19254): #26     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2956:14)

还有Json文件

[{"body":"for test","subject":"KSA","name":{"circumfernce":"33c","Twidth":"6c","height":"55c","Lwidth":"22c"}},{"body":"for test1","subject":"USA","name":{"circumfernce":"313c","Twidth":"61c","height":"515c","Lwidth":"212c"}},{"body":"for test2","subject":"UAE","name":{"circumfernce":"333c","Twidth":"36c","height":"535c","Lwidth":"232c"}}]

0 个答案:

没有答案