函数’StateProvider’未定义。尝试导入定义’StateProvider’的库。

huangapple go评论54阅读模式
英文:

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 &#39;StateProvider&#39; isn&#39;t defined.
Try importing the library that defines &#39;StateProvider&#39;, correcting the name to the name of an existing function, or defining a function named &#39;StateProvider&#39;.

This is my code:

import &#39;package:flutter/material.dart&#39;;
import &#39;package:flutter_riverpod/flutter_riverpod.dart&#39;;
import &#39;package:riverpod/home_screen.dart&#39;;

// final nameProvider = Provider.family&lt;String&gt;((ref, ) =&gt; &#39;caro&#39;);

 final nameProvider = StateProvider((ref)=&gt; &#39;caro&#39;);

// final nameProvider = Provide&lt;String&gt;((ref) =&gt; &#39;caro&#39;);
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: &#39;Flutter Demo&#39;,
      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 &#39;package:riverpod/home_screen.dart&#39;; as that is not part of the "real" riverpod package.

huangapple
  • 本文由 发表于 2023年2月10日 04:49:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定