类型“示例”不是“示例”的类型“示例”的子类型

时间:2019-12-15 15:45:56

标签: flutter dart

我从使用这种语言开始,尝试创建一个可以从服务器接收消息(通过websocket)并将其显示在ListView中的应用程序。

我在专用文件中创建了一个类,以将该消息转换为实体。

但是,从痕迹看来,我在多个文件中使用的类类型似乎并不是...“相同”。

跟踪

E/flutter ( 9655): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type 'Example' is not a subtype of type 'Example' of 'example' where
E/flutter ( 9655):   Example is from package:exampleProject/components/Example.dart
E/flutter ( 9655):   Example is from package:exampleProject/components/example.dart
E/flutter ( 9655): 
E/flutter ( 9655): #0      Plugin._onMessageReceived.<anonymous closure> (package:exampleProject/plugin.dart:35:15)
E/flutter ( 9655): #1      Iterable.forEach (dart:core/iterable.dart:279:30)
E/flutter ( 9655): #2      Plugin._onMessageReceived (package:exampleProject/plugin.dart:31:16)
E/flutter ( 9655): #3      WebSocketsNotifications._onReceptionOfMessageFromServer.<anonymous closure> (package:exampleProject/websockets.dart:95:15)
E/flutter ( 9655): #4      Iterable.forEach (dart:core/iterable.dart:279:30)
E/flutter ( 9655): #5      WebSocketsNotifications._onReceptionOfMessageFromServer (package:exampleProject/websockets.dart:94:16)
E/flutter ( 9655): #6      _rootRunUnary (dart:async/zone.dart:1134:38)
E/flutter ( 9655): #7      _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter ( 9655): #8      _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter ( 9655): #9      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:338:11)
E/flutter ( 9655): #10     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:265:7)
E/flutter ( 9655): #11     _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:134:11)
E/flutter ( 9655): #12     _ForwardingStream._handleData (dart:async/stream_pipe.dart:100:10)
E/flutter ( 9655): #13     _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:166:13)
E/flutter ( 9655): #14     _rootRunUnary (dart:async/zone.dart:1134:38)
E/flutter ( 9655): #15     _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter ( 9655): #16     _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter ( 9655): #17     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:338:11)
E/flutter ( 9655): #18     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:265:7)
E/flutter ( 9655): #19     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:766:19)
E/flutter ( 9655): #20     _StreamController._add (dart:async/stream_controller.dart:642:7)
E/flutter ( 9655): #21     _rootRunUnary (dart:async/zone.dart:1134:38)
E/flutter ( 9655): #22     _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter ( 9655): #23     _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter ( 9655): #24     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:338:11)
E/flutter ( 9655): #25     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:265:7)
E/flutter ( 9655): #26     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:766:19)
E/flutter ( 9655): #27     _StreamController._add (dart:async/stream_controller.dart:642:7)
E/flutter ( 9655): #28     _StreamController.add (dart:async/stream_controller.dart:588:5)
E/flutter ( 9655): #29     new _WebSocketImpl._fromSocket.<anonymous closure> (dart:_http/websocket_impl.dart:1143:21)
E/flutter ( 9655): #30     _rootRunUnary (dart:async/zone.dart:1134:38)
E/flutter ( 9655): #31     _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter ( 9655): #32     _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter ( 9655): #33     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:338:11)
E/flutter ( 9655): #34     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:265:7)
E/flutter ( 9655): #35     _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:70:11)
E/flutter ( 9655): #36     _EventSinkWrapper.add (dart:async/stream_transformers.dart:17:11)
E/flutter ( 9655): #37     _WebSocketProtocolTransformer._messageFrameEnd (dart:_http/websocket_impl.dart:336:22)
E/flutter ( 9655): #38     _WebSocketProtocolTransformer.add (dart:_http/websocket_impl.dart:231:46)
E/flutter ( 9655): #39     _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:122:24)
E/flutter ( 9655): #40     _rootRunUnary (dart:async/zone.dart:1134:38)
E/flutter ( 9655): #41     _CustomZone.runUnary (dart:async/zone.dart:1031:19)
E/flutter ( 9655): #42     _CustomZone.runUnaryGuarded (dart:async/zone.dart:933:7)
E/flutter ( 9655): #43     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:338:11)
E/flutter ( 9655): #44     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:265:7)
E/flutter ( 9655): #45     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:766:19)
E/flutter ( 9655): #46     _StreamController._add (dart:async/stream_controller.dart:642:7)
E/flutter ( 9655): #47     _StreamController.add (dart:async/stream_controller.dart:588:5)
E/flutter ( 9655): #48     _Socket._onData (dart:io-patch/socket_patch.dart:1831:41)
E/flutter ( 9655): #49     _rootRunUnary (dart:async/zone.dart:1138:13)
E/flutter ( 9655): #50     _CustomZone.runUn

这是我的实体课程

// example.dart

class Example {
  String name;

  Example.fromJson(Map<String, dynamic> decodedJson) {
    name = decodedJson['name'];
  }
}

这是与套接字管理器通信并从中接收消息并将内容转换为实体的插件

// plugin.dart

import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:exampleProject/websockets.dart';
import 'package:exampleProject/components/Example.dart';

Plugin plugin = new Plugin();

class Plugin {
  static final Plugin _plugin = new Plugin._internal();

  factory Plugin() => _plugin;

  Plugin._internal() {
    // Initialize sockets
    sockets.initCommunication();

    sockets.addListener(_onMessageReceived);
  }

  _onMessageReceived(message) {
    Map<String, dynamic> decodedJson = json.decode(message);

    _listeners.forEach((Function callback) {

      // --------------------------------------------------------------< Creating the class instance
      Example example = Example.fromJson(decodedJson);
      // --------------------------------------------------------------< Send it to listeners
      callback(example);
    });
  }

  ObserverList<Function> _listeners = new ObserverList<Function>();

  addListener(Function callback) {
    _listeners.add(callback);
  }

  removeListener(Function callback) {
    _listeners.remove(callback);
  }
}

我的主要人

// main.dart

import 'package:flutter/material.dart';

import 'package:exampleProject/websockets.dart';
import 'package:exampleProject/plugin.dart';
import 'package:exampleProject/components/Example.dart';

void main() => runApp(App());

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  // --------------------------------------------------------------< List
  List<Example> examples = [];

  @override
  void initState() {
    super.initState();

    plugin.addListener(_onMessageReceived);
  }

  @override
  void dispose() {
    plugin.removeListener(_onMessageReceived);
    super.dispose();
  }

  // --------------------------------------------------------------< List insert
  _onMessageReceived(Example example) {
    examples.insert(0, example);
    // Force render
    setState(() {});
  }

  [...]
}

我想念什么?

提前

谢谢

1 个答案:

答案 0 :(得分:0)

此行:

Should the link be opened with Chrome or with 'MyApp'?

应为:

import 'package:exampleProject/components/Example.dart';

因为这就是所谓的。类名是import 'package:exampleProject/components/example.dart'; ,但文件名不是。