英文:
The function 'StateProvider' isn't defined. Try importing the library that defines 'StateProvider',
问题
我遇到了这个错误:
函数 'StateProvider' 未定义。
尝试导入定义 'StateProvider' 的库,将名称更正为现有函数的名称,或定义一个名为 'StateProvider' 的函数。
这是我的代码:
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod/home_screen.dart';
// final nameProvider = Provider.family<String>((ref, ) => 'caro');
final nameProvider = StateProvider((ref) => 'caro');
// final nameProvider = Provide<String>((ref) => 'caro');
void main() {
runApp(const ProviderScope(child: MyApp()));
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
useMaterial3: true,
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
英文:
I got this error:
The function 'StateProvider' isn't defined.
Try importing the library that defines 'StateProvider', correcting the name to the name of an existing function, or defining a function named 'StateProvider'.
This is my code:
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod/home_screen.dart';
// final nameProvider = Provider.family<String>((ref, ) => 'caro');
final nameProvider = StateProvider((ref)=> 'caro');
// final nameProvider = Provide<String>((ref) => 'caro');
void main() {
runApp(const ProviderScope(child: MyApp()));
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
useMaterial3: true,
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
Why is this happening if I'm using flutter_riverpod: ^2.1.3
?
How can I solve this issue?
Thanks for any help you can provide
答案1
得分: 2
我看不出有任何问题。我已经尝试过你的代码。
也许只需要运行一下这个命令:flutter pub get
。
唯一我不清楚的是第三个导入是什么。即import 'package:riverpod/home_screen.dart';
,因为它不是“真正”的riverpod
包的一部分。
英文:
I see no fault. I've tried your code myself.
Perhaps a simple run of the command: flutter pub get
.
The only thing I don't know is what the third import is. I.e. import 'package:riverpod/home_screen.dart';
as that is not part of the "real" riverpod
package.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论