无法在包含 StatefulWidget 的 flutter 文件中添加 mongodart

Can't add mongodart in a flutter file which contains a StatefulWidget

我当然是第一个问这个问题的人,因为我在互联网上找不到任何关于这个问题的信息。当我尝试在我的 flutter 应用程序的主文件中导入 mongodart 时,我在 IDE (VsCode) 中遇到了这个错误,而且当我尝试 运行 项目时flutter run:

The name 'State' is defined in the libraries 'framework.dart' and 'mongo_dart.dart'.

更多:

Compiler message: lib/main.dart:37:32: Error: Expected 0 type arguments. class _MyHomePageState extends State { ^ lib/main.dart:34:20: Error: The return type of the method 'MyHomePage::createState' is

lib1::_MyHomePageState, which does not match the return type of the overridden method (#lib2::State<#lib2::StatefulWidget>). Change to a

subtype of #lib2::State<#lib2::StatefulWidget>. _MyHomePageState createState() => new _MyHomePageState(); ^ file:///home/quentin/flutter/packages/flutter/lib/src/widgets/framework.dart:1: Error: This is the overridden method ('createState'). lib/main.dart:41:5: Error: Method not found: 'setState'. setState(() { ^^^^^^^^ lib/main.dart:41:5: Error: The method 'setState' isn't defined for the class '#lib1::_MyHomePageState'. Try correcting the name to the name of an existing method, or defining a method named 'setState'. setState(() { ^ lib/main.dart:63:25: Error: Getter not found: 'widget'. title: new Text(widget.title), ^^^^^^ lib/main.dart:63:25: Error: The getter 'widget' isn't defined for the class '#lib1::_MyHomePageState'. Try correcting the name to the name of an existing getter, or defining a getter or field named 'widget'. title: new Text(widget.title),

注意:StatefulWidget 是示例应用程序中的那个。

我确定这个问题是由 mongodart 引起的,因为当我删除时:

import 'package:mongo_dart/mongo_dart.dart';

任何形式的错误都会消失。 Here's 导致问题的代码。

是的,在名为 "State" 的 mongo 包中有 class 导致冲突,您可以为该包使用别名,如下所示。

import 'package:mongo_dart/mongo_dart.dart' as mongo;

如果您想访问任何 mongo_dart classes,只需使用:

mongo.NameOfTheClass