首页 > 解决方案 > 找不到正确的 Provider 。您尝试阅读的提供者处于不同的路线

问题描述

我的应用程序中有以下路线:

Main.dart ---> SplashScreen.dart ---> DetailsPage.dart

主要.dart

void main() {
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => FontSizeHandler()),
      ],
      child: MyApp(),
    ),
  );
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SplashScreen(),
    );
  }
}

SplashScreen.dart我移动到DetailsPage.dart使用 Navigator.pushAndRemoveUntil

Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (context) => DetailsPage()), (route) => false);

现在在App Bar 的详细信息页面中,有一个图标,按下它我想使用FontSizeHandler更改字体

详细信息页面.dart

class DetailsPage extends StatelessWidget {
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.arrow_upward),
            onPressed: () {
              context.read<FontSizeHandler>().increaseFont();
            },
          ),
          IconButton(
            onPressed: () {
              context.read<FontSizeHandler>().decreaseFont();
            },
            icon: Icon(Icons.arrow_downward),
          ),
        ],
        title: Text(
          "DetailsPage",
          style: GoogleFonts.roboto(),
        ),
      ),
      body: SafeArea(
        child: SingleChildScrollView(
          child: Card(
            child: Container(
              padding: EdgeInsets.fromLTRB(5, 5, 5, 15),
              child: AutoSizeText(
                "MyTexts",
                textAlign: TextAlign.justify,
                style: GoogleFonts.openSans(
                  fontSize:
                      context.watch<FontSizeHandler>().fontSize.toDouble(),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

所以这里的问题是我收到了这个错误信息

找不到正确的提供程序 这可能是因为您使用了BuildContext不包含提供程序的

这个错误是因为我使用了Navigator.pushAndRemoveUntil吗?虽然我在层次结构顶部有ChangeNotifierProvider,为什么会抛出错误?如何解决这个问题?

字体大小处理程序.dart

class FontSizeHandler with ChangeNotifier {
  int fontSize = 15;
  void increaseFont() {
    fontSize = fontSize + 2;
    notifyListeners();
  }

  void decreaseFont() {
    fontSize = fontSize - 2;
    notifyListeners();
  }
}

已解决:问题在于导入错误的 ChangeNotifier 类。再也不相信自动导入

标签: flutterflutter-provider

解决方案


更新的答案

作为参考,该问题是由以下错误导入引起的:

import 'file:///.../fontchangehandler.dart'; // import 'package:.../fontchangehandler.dart';

原始答案

不幸的是,我无法重现您使用提供的代码遇到的错误。如果您可以提供DetailsPage该类的其余代码,那么这可能有助于进一步诊断错误。我能够使下面的示例正常工作,希望您会发现它有用:

import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider<FontSizeHandler>(
          create: (context) {
            return FontSizeHandler();
          },
        ),
      ],
      child: MyApp(),
    ),
  );
}

class FontSizeHandler extends ChangeNotifier {
  int _fontSize = 15;

  int get fontSize => _fontSize;

  void increaseFont() {
    _fontSize = _fontSize + 2;

    notifyListeners();
  }

  void decreaseFont() {
    _fontSize = _fontSize - 2;

    notifyListeners();
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SplashScreen(),
    );
  }
}

class SplashScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: RaisedButton(
          onPressed: () async {
            await Navigator.pushAndRemoveUntil(
              context,
              MaterialPageRoute(
                builder: (context) {
                  return DetailsPage();
                },
              ),
              (route) => false,
            );
          },
          child: Text('GO TO DETAILS'),
        ),
      ),
    );
  }
}

class DetailsPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.arrow_upward),
            onPressed: () {
              context.read<FontSizeHandler>().increaseFont();
            },
          ),
          IconButton(
            onPressed: () {
              context.read<FontSizeHandler>().decreaseFont();
            },
            icon: Icon(Icons.arrow_downward),
          ),
        ],
        title: Text(
          "DetailsPage",
          style: GoogleFonts.roboto(),
        ),
      ),
      body: SafeArea(
        child: SingleChildScrollView(
          child: Card(
            child: Container(
              padding: EdgeInsets.fromLTRB(5, 5, 5, 15),
              child: AutoSizeText(
                "MyTexts",
                textAlign: TextAlign.justify,
                style: GoogleFonts.openSans(
                  fontSize: context.watch<FontSizeHandler>().fontSize.toDouble(),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

推荐阅读