错误:无法将类型'List <#lib1 :: Data>'的值分配给'List <#lib2 :: Data>类型的变量

时间:2018-07-12 14:35:06

标签: dart flutter

使用此api创建地震信息应用程序: https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson

但是在运行应用程序后出现此错误:

  

错误:不能将类型为'dart.core :: List <#lib1 :: Data>'的值分配给类型为'dart.core :: List <#lib2 :: Data>'的变量。 / p>      

E / flutter(9986):尝试更改左侧的类型,或将右侧投射为'dart.core :: List <#lib2 :: Data>'。

     

E / flutter(9986):_quakes = quakes;

我的拍打版本:Flutter 0.5.7-pre.100 • channel master

代码:api.dart

import 'dart:async';
import 'dart:convert';
import 'package:flutter_layout/project/quake/models/quake.dart';
import 'package:flutter_layout/project/quake/models/serializers.dart';

import 'package:http/http.dart' as http;

const String baseUrl = 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson';

Future<List<Data>> getQuake() async {
  final response  = await http.get((Uri.parse(baseUrl)));
  print("${response.toString()}");

  Quake quake = serializers.deserializeWith(Quake.serializer, json.decode(response.body));

  return quake.features.map((Properties properties) => properties.properties).toList();
}

quake.dart

import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';

part 'quake.g.dart';

abstract class Quake implements Built<Quake, QuakeBuilder> {
  BuiltList<Properties> get features;
  Quake._();
  factory Quake([updates(QuakeBuilder b)]) = _$Quake;
  static Serializer<Quake> get serializer => _$quakeSerializer;
}

abstract class Properties implements Built<Properties, PropertiesBuilder> {
  Data get properties;
  Properties._();
  factory Properties([updates(PropertiesBuilder b)]) = _$Properties;
  static Serializer<Properties> get serializer => _$propertiesSerializer;
}

abstract class Data implements Built<Data, DataBuilder> {
  double get mag;
  String get place;
  Data._();
  factory Data([updates(DataBuilder b)]) = _$Data;
  static Serializer<Data> get serializer => _$dataSerializer;
}

QuakeApp.dart

import 'package:flutter/material.dart';
import 'QuakeListView.dart';
import 'models/api.dart';
import 'models/quake.dart';

class QuakeApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return QuakeAppState();
  }
}

class QuakeAppState extends State<QuakeApp>{
  List<Data> _quakes = [];

  @override
  void initState() {
    super.initState();
    getQuake().then((quakes){
      _quakes = quakes;
    });
  }

  List<QuakeListItem> buildListWidget(){
    return _quakes.map((post) => QuakeListItem(
      mag: post.mag,
      place: post.place,
    )).toList();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Quake App"),
        centerTitle: true,
      ),
      body: RefreshIndicator(
        onRefresh: () async{
          getQuake().then((quakes){
            setState(() {
              _quakes = quakes;
              print("${quakes.length}");
            });
          });
        },
        child: AnimatedCrossFade(
          duration: Duration(microseconds: 300),
          firstChild: Center(
            child: CircularProgressIndicator(),
          ),
          secondChild: ListView(
            children: buildListWidget(),
          ),
          crossFadeState: _quakes != null ? CrossFadeState.showSecond : CrossFadeState.showFirst,
        ),
      ),
    );
  }
}

有解决方案吗?

1 个答案:

答案 0 :(得分:3)

我猜这是已知问题https://github.com/dart-lang/sdk/issues/33076

不要在lib/main.dart中使用相对导入(仅'package:...'导入)
从任何其他文件导入lib/main.dart