在 flutter 中使用 changeNotifierProvider 时无法反映变化

Unable to reflect changes while using changeNotifierProvider in flutter

devs 我正在开发 flutter 应用程序并使用 provider 进行状态管理。我想从单个 class AppState 管理应用程序状态,但是当我使用这种方法时,更改不会反映在 UI.I 中,我正在编写以下代码:

我想在 ClassA 中反映变化,而递增值的函数正在从 DummyService Class 调用。

main.dart

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => SignUpBusinessLogic()),
        ChangeNotifierProvider(create: (_) => AppState()),
        ChangeNotifierProvider(
          create: (_) => AuthServices.instance(),
        ),
        StreamProvider(
          create: (context) => context.read<AuthServices>().authState,
        )
      ],
      child: MaterialApp(
        title: 'Flutter Demo',
        // initialRoute: Routes.WRAPPER_ROUTE,
        // onGenerateRoute: RouteGenerator.generateRoute,
        theme: ThemeData(
          textTheme: GoogleFonts.poppinsTextTheme(Theme.of(context).textTheme),
          scaffoldBackgroundColor: Color(0xffF0F4FD),
          buttonColor: FrontEndConfigs.appBaseColor,
          iconTheme: IconThemeData(color: FrontEndConfigs.appBaseColor),
          primaryColor: FrontEndConfigs.appBaseColor,
          floatingActionButtonTheme: FloatingActionButtonThemeData(
              backgroundColor: FrontEndConfigs.appBaseColor),
          visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
        home: ClassA(),
      ),
    );
  }
}

classA.dart

class ClassA extends StatelessWidget {
  DummyService _dummyService = DummyService();
  @override
  Widget build(BuildContext context) {
    var _status = Provider.of<AppState>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text("Class A"),
      ),
      body: Center(
        child: Text(_status.getA().toString()),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          _dummyService.dummyData();
        },
      ),
    );
  }
}

app_state.dart

class AppState with ChangeNotifier {

  int a = 0;

  incrementA(int i) {
    a++;
    notifyListeners();
  }

  getA() => a;
}

services.dart

class DummyService {
  AppState _appState = AppState();
  Future<void> dummyData() async {
    _appState.incrementA(0);
  }}

在您的 ClassA 中,您创建了一个 DummyService,它创建了 AppState 的一个新实例。因此,当您单击 FloatingActionButton 时,您不会增加 ChangeNotifierProvider 提供的 AppState

试试这个:

class ClassA extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var _status = Provider.of<AppState>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text("Class A"),
      ),
      body: Center(
        child: Text(_status.getA().toString()),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          Provider.of<AppState>(context, listen: false).incrementA(0);
        },
      ),
    );
  }
}