当监听Flutter Provider暴露的值时,Widget不会重建

6za6bjd0  于 2023-05-29  发布在  Flutter
关注(0)|答案(1)|浏览(154)

在我的代码中,我将使用Provider.value向MyHomePage公开一个值,同时,MyHomePage将使用Provider来获取监听设置为true的值,如果我在外部更改该值,MyHomePage将不会重建,似乎监听不起作用。

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

void main() {
  runApp(const MyApp());
}

int _counter = 0;

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(
        primarySwatch: Colors.blue,
      ),
      home: Provider.value(
        value: _counter,
        child: MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);
  void _incrementCounter() {
    _counter++;
  }

  @override
  Widget build(BuildContext context) {
    print('build');
    int value = Provider.of<int>(
      context,
      listen: true,
    );
    return Scaffold(
      appBar: AppBar(
        title: Text('123'),
      ),
      body: Center(
        child: Text('value:${value}'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
gajydyqb

gajydyqb1#

如果您希望小部件重新构建,则需要使用一个能够通知其侦听器的模型。Provider无法知道您递增了_counted
执行这些代码更改以使用ValueNotifier<int>而不是int(您还需要将Provider替换为ChangeNotifierProvider):

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

void main() {
  runApp(const MyApp());
}

final _counter = ValueNotifier<int>(0); // <- Here

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(
        primarySwatch: Colors.blue,
      ),
      home: ChangeNotifierProvider.value(
        value: _counter,
        child: MyHomePage(),
      ),

    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);
  void _incrementCounter() {
    _counter.value++;
  }

  @override
  Widget build(BuildContext context) {
    print('build');
    int value = Provider.of<ValueNotifier<int>>(
      context,
      listen: true,
    ).value;
    return Scaffold(
      appBar: AppBar(
        title: Text('123'),
      ),
      body: Center(
        child: Text('value:${value}'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

最好不要使用静态全局变量和ChangeNotifierProvider.value,而是使用ChangeNotifierProvider.new

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

void main() {
  runApp(const 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(
        primarySwatch: Colors.blue,
      ),
      // Here:
      home: ChangeNotifierProvider(
        create: (context) => ValueNotifier<int>(0),
        child: MyHomePage(),
      ),

    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);
  void _incrementCounter(BuildContext context) {
    context.read<ValueNotifier<int>>().value++;
  }

  @override
  Widget build(BuildContext context) {
    print('build');
    int value = Provider.of<ValueNotifier<int>>(
      context,
      listen: true,
    ).value;
    return Scaffold(
      appBar: AppBar(
        title: Text('123'),
      ),
      body: Center(
        child: Text('value:${value}'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

相关问题