无法设置使用提供程序Flutter从Firebase数据库获取的数据



我正试图从我的fierbase数据库中获取数据(对象/字典列表),但我无法使用provider设置状态。

这是我的firebase数据库结构:

historical_data: [
  {key: val, key: val, ...},
  {key: val, key: val, ...},
  ...
],
live_data: {
  key: val,
  key: val,
  ...
}

提供者文件:

class StockProvider with ChangeNotifier {
  List<ChartHistoricalData> _histChartData = [];
  List<ChartHistoricalData> get histChartData => _histChartData;
  Future<void> fetchHistoricalStockData(ref) async {
    await ref.once().then((DataSnapshot data) {
      print(data.value['historical_data']); // Working at this point, I can see my list of objects.
      _histChartData = List<ChartHistoricalData>.from(data
          .value['SBIN_Historical']
          .map((x) => ChartHistoricalData.fromJson(x as Map<String, dynamic>)).toList());
    });
    notifyListeners();
  }
}

My model class:

class ChartHistoricalData {
  DateTime? x; //DateTime
  double? open;
  double? high;
  double? low;
  double? close;
  ChartHistoricalData(
      {required this.x,
      required this.open,
      required this.high,
      required this.low,
      required this.close});
  ChartHistoricalData.fromJson(Map<String, dynamic> json) {
    x = DateTime.parse(json['date']); //date of type string coming from firebase db
    open = json['open'];
    high = json['high'];
    low = json['low'];
    close = json['close'];
  }
  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['date'] = this.x;
    data['open'] = this.open;
    data['high'] = this.high;
    data['low'] = this.low;
    data['close'] = this.close;
    return data;
  }
}

主界面:

class SBINChart extends StatefulWidget {
  const SBINChart({Key? key}) : super(key: key);
  @override
  _ChartState createState() => _ChartState();
}
class _ChartState extends State<Chart> {
  final fb = FirebaseDatabase.instance;
  @override
  Widget build(BuildContext context) {
    final ref = fb.reference();
    context.read<StockProvider>().fetchHistoricalStockData(ref);
    print(context.watch<StockProvider>().histChartData); // Empty
    ...

错误:

/flutter ( 4730): [{date: 2021-01-01, high: 280.0, low: 274.4, close: 279.4, open: 274.9}, {date: 2021-01-04, high: 283.9, low: 277.75, close: 281.05, open: 281.85}, {date: 2021-01-05, high: 282.45, low: 277.0, close: 281.75, open: 278.05}, {date: 2021-01-06, high: 289.15, low: 281.4, close: 285.05, open: 283.0}, {date: 2021-01-07, high: 291.8, low: 287.0, close: 287.7, open: 289.0}, {date: 2021-01-08, high: 291.4, low: 285.2, close: 286.0, open: 290.1}, {date: 2021-01-11, high: 288.2, low: 279.6, close: 282.5, open: 288.0}, {date: 2021-01-12, high: 293.85, low: 277.9, close: 292.5, open: 280.0}, {date: 2021-01-13, high: 308.0, low: 294.5, close: 306.8, open: 296.0}, {date: 2021-01-14, high: 309.25, low: 303.8, close: 307.25, open: 306.7}, {date: 2021-01-15, high: 310.9, low: 301.3, close: 303.85, open: 306.8}, {date: 2021-01-18, high: 308.65, low: 292.2, close: 294.45, open: 303.5}, {date: 2021-01-19, high: 302.5, low: 296.4, close: 298.6, open: 297.65}, {date: 2021-01-20, high: 304.7, low: 296.85, close: 302.55, open: 298.8
E/flutter ( 4730): [ERROR:flutter/shell/common/shell.cc(103)] Dart Unhandled Exception: type '_InternalLinkedHashMap<Object?, Object?>' is not a subtype of type 'Map<String, dynamic>' in type cast, stack trace: #0      StockProvider.fetchHistoricalStockData.<anonymous closure>.<anonymous closure>
package:algoguru/providers/stock_provider.dart:16
E/flutter ( 4730): #1      MappedListIterable.elementAt (dart:_internal/iterable.dart:412:31)
E/flutter ( 4730): #2      ListIterator.moveNext (dart:_internal/iterable.dart:341:26)
E/flutter ( 4730): #3      new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:188:27)
E/flutter ( 4730): #4      new _GrowableList.of (dart:core-patch/growable_array.dart:150:28)
E/flutter ( 4730): #5      new List.of (dart:core-patch/array_patch.dart:50:28)
E/flutter ( 4730): #6      ListIterable.toList (dart:_internal/iterable.dart:212:44)
E/flutter ( 4730): #7      StockProvider.fetchHistoricalStockData.<anonymous closure>
package:algoguru/providers/stock_provider.dart:17
E/flutter ( 4730): <asynchronous suspension>
E/flutter ( 4730): #8      StockProvider.fetchHistoricalStockData
package:algoguru/providers/stock_provider.dart:12
E/flutter ( 4730): <asynchronous suspension>

我认为ChartHistoricalData.fromJson方法可能有问题,但由于我是新的扑动,我不知道出了什么问题。如果有人能帮忙,那就太好了!

在你的provider中修改:

.map((x) => ChartHistoricalData.fromJson(x as Map<String, dynamic>)));

仔细阅读你得到的错误。它告诉你这是一个类型不匹配错误,意思是它期望的某种类型是Map<String, dynamic>,但你得到的是<Object?, Object?>类型的东西。

虽然它们内部是相同的数据,但只有你知道。它不知道。dart从哪里得到错误object?来显示给我们?它来自Firebase的软件包。因为他们知道它肯定是一个物体,可以是任何东西。

但是对于你,你告诉你的方法那些对象将显式地是String, dynamic。否则,抛出一个错误,这正是所发生的。

这会导致错误。我提出的解决方案被称为casting,其中您告诉dart,我们传递的变量x将被视为Map<String, dynamic>对象,因此处理它并相应地解析它。

Dart会说好的,很酷。但是如果它得到的对象不是Map<String, dynamic>对象,它会抛出一个不同的错误,说你所要求的是不可能的,因为强制转换两边的对象是不同的。

Future<void> fetchHistoricalStockData(ref) async {
    await ref.once().then((DataSnapshot data) {
      print(data.value['historical_data']); // Working at this point, I can see my list of objects.
      _histChartData = List<ChartHistoricalData>.from(jsonDecode(data
          .value['SBIN_Historical'])
          .map((x) => ChartHistoricalData.fromJson(x as Map<String, dynamic>)).toList());
    });
    notifyListeners();
  }

相关内容

  • 没有找到相关文章

最新更新