Flutter提供者中的选择器不起作用

时间:2020-03-06 06:09:00

标签: flutter flutter-provider

我使用提供程序4.0.3

我的伪代码是

class MyModel with ChangeNotifier
User user //{nickname: 'john', age: 1}
//...



ChangeNotifierProvider<MyModel>
(create : (_)=>MyModel()
child: //MyModel model = Provider.of(context);
scafold
 body: Column(
   [
      Selector<MyModel, String>(
        selector : (_, m) => m.user.nickname,
        builder : (_,nickname,___) => Text(nickname)
      )
      RaisedButton(onPressed:() => model.user.nickname = 'none' )
   ]
)
//...

我希望

“已举起RaisedButton->更改昵称。”

但它不起作用。

我错过了什么?

2 个答案:

答案 0 :(得分:1)

您可以在

下复制粘贴运行完整代码

代码段

Selector<ModelProvider, User>(
              builder: (context, data, child) {
                return Text('${data.nickName}');
              },
              selector: (buildContext, modelProvider) => modelProvider.getUser,
            )
...
class ModelProvider extends ChangeNotifier {
  User user = User(nickName: "john", age: 1);

  User get getUser => user;

  setUserNickName(String newNickName) {
    user.nickName = newNickName;
    print(user.nickName);
    notifyListeners();
  }
}

工作演示

enter image description here

完整代码

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (BuildContext context) => ModelProvider(),
        ),
      ],
      child: MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.teal,
          ),
          home: MyHomePage()),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    final modelProvider = Provider.of<ModelProvider>(context, listen: true);
    return SafeArea(
      child: Scaffold(
        body: Center(
            child: Column(
          children: <Widget>[
            Selector<ModelProvider, User>(
              builder: (context, data, child) {
                return Text('${data.nickName}');
              },
              selector: (buildContext, modelProvider) => modelProvider.getUser,
            )
          ],
        )),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            modelProvider.setUserNickName("new name");
          },
        ),
      ),
    );
  }
}

class ModelProvider extends ChangeNotifier {
  User user = User(nickName: "john", age: 1);

  User get getUser => user;

  setUserNickName(String newNickName) {
    user.nickName = newNickName;
    print(user.nickName);
    notifyListeners();
  }
}

class User {
  String nickName;
  int age;

  User({this.nickName, this.age});
}

答案 1 :(得分:0)

此答案中没有Selector的含义, 如果使用

,整个树会重建
final modelProvider = Provider.of<ModelProvider>(context, listen: true);

正确的答案是,如果更改对象的属性,选择器将不起作用。