错误:在此LoginScreen小部件上方找不到正确的Provider<UserProvider>。

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

Error: Could not find the correct Provider<UserProvider> above this LoginScreen Widget

问题

这个屏幕是一个登录屏幕,用户可以在这里进入应用程序。尽管我使用了正确的提供程序,但我遇到了这个错误

>{错误:无法在此 Z Widget 上找到正确的 Provider<X>

>要修复,请:
>

  • 确保 Provider<X> 是这个 Z Widget 的祖先
  • 为 Provider<X> 提供类型
  • 为 Consumer<X> 提供类型
  • 为 Provider.of<X>() 提供类型
  • 始终使用包导入。例如:import &#39;package:my_app/my_code.dart&#39;;
  • 确保正确的 context 被使用。

我想知道为什么使用 Provider.of(context) 不起作用

    import &#39;package:provider/provider.dart&#39;;
    import &#39;package:voyager/api/userProvider.dart&#39;;

    class LoginScreen extends StatefulWidget {
      
    @override
    _LoginScreenState createState() =&gt; _LoginScreenState();
    }
    
    class _LoginScreenState extends State&lt;LoginScreen&gt; {
    UserProvider user_Provider;
    
   

     @override
       Widget build(BuildContext context) {
        userProvider = Provider.of&lt;UserProvider&gt;(context,listen: false);
        return MultiProvider(
          providers: [
            ChangeNotifierProvider(create: (_) =&gt; UserProvider()),
          ],
        child: Consumer&lt;UserProvider&gt;(
          builder: (context, userProvider, child) =&gt;  Container(
            child: Scaffold(
                body: InkWell(
                  splashColor: Colors.transparent,
                  highlightColor: Colors.transparent,
                  focusColor: Colors.transparent,
                  onTap: () {

闪屏代码

 Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        backgroundColor: AppTheme.getTheme().backgroundColor,
        body:Column(
          children: &lt;Widget&gt;[
            SizedBox(
              height: MediaQuery.of(context).padding.top,
            ),
            Expanded(
              child: PageView(
                controller: pageController,
                pageSnapping: true,
                onPageChanged: (index) {
                  currentShowIndex = index;
                },
                scrollDirection: Axis.horizontal,
                children: &lt;Widget&gt;[
                  PagePopup(imageData: pageViewModelData[0]),
                  PagePopup(imageData: pageViewModelData[1]),
                  PagePopup(imageData: pageViewModelData[2]),
                ],
              ),
            ),
            PageIndicator(
              layout: PageIndicatorLayout.WARM,
              size: 10.0,
              controller: pageController,
              space: 5.0,
              count: 3,
              color: AppTheme.getTheme().dividerColor,
              activeColor: AppTheme.getTheme().primaryColor,
            ),
            Padding(
              padding: const EdgeInsets.only(left: 48, right: 48, bottom: 8, top: 32),
              child: Container(
                height: 48,
                decoration: BoxDecoration(
                  color: AppTheme.getTheme().primaryColor,
                  borderRadius: BorderRadius.all(Radius.circular(24.0)),
                  boxShadow: &lt;BoxShadow&gt;[
                    BoxShadow(
                      color: AppTheme.getTheme().dividerColor,
                      blurRadius: 8,
                      offset: Offset(4, 4),
                    ),
                  ],
                ),
                child: Material(
                  color: Colors.transparent,
                  child: InkWell(
                    borderRadius: BorderRadius.all(Radius.circular(24.0)),
                    highlightColor: Colors.transparent,
                    onTap: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) =&gt; LoginScreen()),
                      );
                    },

<details>
<summary>英文:</summary>

This screen is a Login screen which makes the user enter into the application. I got this error although I am using the correct provider

&gt;{Error: Could not find the correct Provider&lt;X&gt; above this Z Widget

&gt;To fix, please:
&gt;
  * Ensure the Provider&lt;X&gt; is an ancestor to this Z Widget
  * Provide types to Provider&lt;X&gt;
  * Provide types to Consumer&lt;X&gt;
  * Provide types to Provider.of&lt;X&gt;()
  * Always use package imports. Ex: `import &#39;package:my_app/my_code.dart&#39;;`
  * Ensure the correct `context` is being used.

I want to know the problem why using `Provider.of(context)` does not work
import &#39;package:provider/provider.dart&#39;;
import &#39;package:voyager/api/userProvider.dart&#39;;

class LoginScreen extends StatefulWidget {
  
@override
_LoginScreenState createState() =&gt; _LoginScreenState();
}

class _LoginScreenState extends State&lt;LoginScreen&gt; {
UserProvider user_Provider;



 @override
   Widget build(BuildContext context) {
    userProvider = Provider.of&lt;UserProvider&gt;(context,listen: false);
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) =&gt; UserProvider()),
      ],
    child: Consumer&lt;UserProvider&gt;(
      builder: (context, userProvider, child) =&gt;  Container(
        child: Scaffold(
            body: InkWell(
              splashColor: Colors.transparent,
              highlightColor: Colors.transparent,
              focusColor: Colors.transparent,
              onTap: () {

splash screen code 

     Widget build(BuildContext context) {
        return Container(
          child: Scaffold(
            backgroundColor: AppTheme.getTheme().backgroundColor,
            body:Column(
              children: &lt;Widget&gt;[
                SizedBox(
                  height: MediaQuery.of(context).padding.top,
                ),
                Expanded(
                  child: PageView(
                    controller: pageController,
                    pageSnapping: true,
                    onPageChanged: (index) {
                      currentShowIndex = index;
                    },
                    scrollDirection: Axis.horizontal,
                    children: &lt;Widget&gt;[
                      PagePopup(imageData: pageViewModelData[0]),
                      PagePopup(imageData: pageViewModelData[1]),
                      PagePopup(imageData: pageViewModelData[2]),
                    ],
                  ),
                ),
                PageIndicator(
                  layout: PageIndicatorLayout.WARM,
                  size: 10.0,
                  controller: pageController,
                  space: 5.0,
                  count: 3,
                  color: AppTheme.getTheme().dividerColor,
                  activeColor: AppTheme.getTheme().primaryColor,
                ),
                Padding(
                  padding: const EdgeInsets.only(left: 48, right: 48, bottom: 8, top: 32),
                  child: Container(
                    height: 48,
                    decoration: BoxDecoration(
                      color: AppTheme.getTheme().primaryColor,
                      borderRadius: BorderRadius.all(Radius.circular(24.0)),
                      boxShadow: &lt;BoxShadow&gt;[
                        BoxShadow(
                          color: AppTheme.getTheme().dividerColor,
                          blurRadius: 8,
                          offset: Offset(4, 4),
                        ),
                      ],
                    ),
                    child: Material(
                      color: Colors.transparent,
                      child: InkWell(
                        borderRadius: BorderRadius.all(Radius.circular(24.0)),
                        highlightColor: Colors.transparent,
                        onTap: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(builder: (context) =&gt; LoginScreen()),
                          );
                        },



</details>


# 答案1
**得分**: 1

你可以在祖先小部件中注册提供程序,例如 `MyApp`,然后让提供程序包装 `MaterialApp(home: LoginScreen())`。

```dart
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => X()),
      ],
      child: Consumer<X>(
        builder: (context, counter, _) {
          return MaterialApp(
            home: LoginScreen(),
          );
        },
      ),
    );
  }
}

完整的测试代码:

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

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

class X with ChangeNotifier {
  int _count = 0;
  int get count => _count;

  void increment() {
    _count++;
    notifyListeners();
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => X()),
      ],
      child: Consumer<X>(
        builder: (context, counter, _) {
          return MaterialApp(
            home: LoginScreen(),
          );
        },
      ),
    );
  }
}

class LoginScreen extends StatelessWidget {
  const LoginScreen({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final userProvider = Provider.of<X>(context);
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '${userProvider.count}',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: IncrementCounterButton(),
    );
  }
}

class IncrementCounterButton extends StatelessWidget {
  const IncrementCounterButton({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return FloatingActionButton(
      onPressed: () {
        Provider.of<X>(context, listen: false).increment();
      },
      tooltip: 'Increment',
      child: const Icon(Icons.add),
    );
  }
}
英文:

You can register provider in ancestor widget like MyApp <br>
and let your provider wrap MaterialApp(home: LoginScreen())

code snippet

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
	return MultiProvider(
	  providers: [
		ChangeNotifierProvider(create: (_) =&gt; X()),
	  ],
	  child: Consumer&lt;X&gt;(
		builder: (context, counter, _) {
		  return MaterialApp(
			home: LoginScreen(),
		  );
		},
	  ),
	);
  }
}

full test code

import &#39;package:flutter/foundation.dart&#39;;
import &#39;package:flutter/material.dart&#39;;
import &#39;package:provider/provider.dart&#39;;

void main() =&gt; runApp(MyApp());

class X with ChangeNotifier {
  int _count = 0;
  int get count =&gt; _count;

  void increment() {
	_count++;
	notifyListeners();
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
	return MultiProvider(
	  providers: [
		ChangeNotifierProvider(create: (_) =&gt; X()),
	  ],
	  child: Consumer&lt;X&gt;(
		builder: (context, counter, _) {
		  return MaterialApp(
			home: LoginScreen(),
		  );
		},
	  ),
	);
  }
}


class LoginScreen extends StatelessWidget {
  const LoginScreen({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
	final user_Provider = Provider.of&lt;X&gt;(context);
	return Scaffold(
	  body: Center(
		child: Column(
		  mainAxisSize: MainAxisSize.min,
		  mainAxisAlignment: MainAxisAlignment.center,
		  children: &lt;Widget&gt;[
			const Text(
			  &#39;You have pushed the button this many times:&#39;,
			),
			Text(
			  &#39;${user_Provider.count}&#39;,
			  style: Theme.of(context).textTheme.display1,
			),
		  ],
		),
	  ),
	  floatingActionButton: IncrementCounterButton(),
	);
  }
}


class IncrementCounterButton extends StatelessWidget {
  const IncrementCounterButton({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
	return FloatingActionButton(
	  onPressed: () {
		Provider.of&lt;X&gt;(context, listen: false).increment();
	  },
	  tooltip: &#39;Increment&#39;,
	  child: const Icon(Icons.add),
	);
  }
}

huangapple
  • 本文由 发表于 2020年1月6日 14:38:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/59607703.html
匿名

发表评论

匿名网友

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

确定