在Flutter中解析地图列表时,如何解决“ .map.tolist()返回实例而不是列表”的问题?

时间:2019-05-14 06:50:02

标签: json parsing dart flutter

我正在尝试读取Firebase实时数据库并将其转换为列表,以便可以在应用程序的表小部件中显示它。

这是我要导入到Firebase实时数据库中然后在我的应用程序中调用的Json数据

[
    {
        "SYMBOL": "VOLTAS",
        "OPEN": "572.8",
        "CLOSE": "572.2",
        "FORECAST": "TRENDING"
    },
    {
        "SYMBOL": "GODREJCP",
        "OPEN": "645.05",
        "CLOSE": "640.55",
        "FORECAST": "TRENDING"
    },
    {
        "SYMBOL": "MARICO",
        "OPEN": "355.4",
        "CLOSE": "351.4",
        "FORECAST": "TRENDING"
    },
    {
        "SYMBOL": "KOTAKBANK",
        "OPEN": "1396.0",
        "CLOSE": "1389.35",
        "FORECAST": "TRENDING"
    },
    {
        "SYMBOL": "KAJARIACER",
        "OPEN": "575.0",
        "CLOSE": "579.95",
        "FORECAST": "TRENDING"
    }
]

我搜索了几个小时,终于找到了本指南: https://medium.com/flutter-community/parsing-complex-json-in-flutter-747c46655f51

我终于明白我正在处理一张地图清单。

下面是我当前的代码集。 cprdata.dart:

class MyDataList {
  final List<MyData> myList ;

  MyDataList({
    this.myList,
  });

  factory MyDataList.fromJson(List<dynamic> parsedJson) {

    List<MyData> myList = new List<MyData>();
    myList = parsedJson.map((i)=> MyData.fromJson(i)).toList();

    return new MyDataList(
        myList: myList
    );
  }
}




class MyData {
  final String SYMBOL;
  final String FORECAST;
  final String OPEN;
  final String CLOSE;

  MyData({
    this.SYMBOL,
    this.OPEN,
    this.CLOSE,
    this.FORECAST});

  factory MyData.fromJson(Map<String, dynamic> json){
    //print(json);
    return new MyData(
      SYMBOL: json['SYMBOL'],
      OPEN: json['OPEN'],
      CLOSE: json['CLOSE'],
      FORECAST: json['FORECAST']
    );
  }

}

cpr.dart

///import 'dart:async';
import 'package:flutter/material.dart';
import 'package:firebase_database/firebase_database.dart';
import 'cprdata.dart';
import 'dart:convert';


class CprAnalysis extends StatefulWidget{
  @override
  CPRState createState() => CPRState();
}

class CPRState extends State<CprAnalysis> {

  List<MyData> allData = [];

  @override
  void initState() {
    super.initState();
    DatabaseReference cprData = FirebaseDatabase.instance.reference();

    cprData.reference().once().then((DataSnapshot snap) {
      var d = snap.value;
      final jsonE = json.encode(d);
      final jsonResponse = json.decode(jsonE);
      MyDataList zz = new MyDataList.fromJson(jsonResponse);
      print(zz.myList);

      setState(() {
        print("Done");
        //print('Length : ${allData.length}');
      });
    }
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("CPR Analysis"),

      ),
    );
  }

预期结果是一个以列表格式包含json文件数据的列表,这样我就可以使用它来制作一个要在我的应用程序上显示的表格。

实际结果:

I/flutter (16067): [Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance of 'MyData', Instance o
I/flutter (16067): Done

2 个答案:

答案 0 :(得分:0)

您要让Dart打印zz的内容,这是MyData实例的列表-因此它在做正确的事情。如果您想查看列表中包含的数据,则可以执行以下操作:

zz.forEach((data) => print(
      "Row contains ${data.SYMBOL} - ${data.FORECAST} - ${data.OPEN} - ${data.CLOSE}"));

答案 1 :(得分:0)

您可以通过覆盖toString()类中的MyData使其打印所需的内容:

class MyData {
  ...

  // Add this
  @override
  String toString() {
    return 'Symbol: $SYMBOL, Open: $OPEN, Close: $CLOSE, Forecast: $FORECAST';
  }
}