I'm certainly the first to ask this question because I found just nothing on the internet about this issue. When I try to import mongodart
in the main file of my flutter application, I got this error in my IDE (VsCode) and also when I try to run the project with flutter run
:
The name 'State' is defined in the libraries 'framework.dart' and 'mongo_dart.dart'.
more:
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),
Note: The StatefulWidget is the one from the sample app.
I'm sure this problem is caused by mongodart
because when I remove:
import 'package:mongo_dart/mongo_dart.dart';
any form of error disappear. Here's the code that causes the problem.
Yes, there is class inside the mongo package named "State" which causes the conflict, you can use an alias for that package, like this.
import 'package:mongo_dart/mongo_dart.dart' as mongo;
And if you want to access to any mongo_dart classes just use :
mongo.NameOfTheClass