具有改变通知器的Flutter提供器

iyfamqjs  于 2023-02-25  发布在  Flutter
关注(0)|答案(1)|浏览(112)

我很难理解提供程序状态管理。我有一个包含以下参数的模型。

我的模型.dart

class MyModel {
  final String title;
  final String details;
  final DateTime startingTime;
  final DateTime endingTime;
  final int categoryId;
  final int userId;

  MyModel(
      this.title,
      this.details,
      this.startingTime,
      this.endingTime,
      this.categoryId,
      this.userId);

}

这些数据中的每一个都引用了Form中的一个位置。(TextFormField,DropDown等)。如果不给它们一个初始值,我如何填充MyModel并确保这些参数中的每一个都被赋给了某个值?(null值也不例外)。
如果我使用这个模型作为ChangeNotifer,并使用setter,getter。那么,我必须在我的MainApp中提供以下代码。

ChangeNotifierProvider<MyModel>(create: (_) => MyModel(*PROVIDEDATA*),),

但这是不可能的,因为我需要提供构造函数参数。我如何找到一种方法来利用这一点呢?
基本上,我需要设置没有默认值的MyModel参数,并通知那些侦听器。我尝试使用MyModelProvider类来提供这个对象。如下所示。但是,我仍然需要提供那些值。

我的模型提供程序.dart

class MyModelProvider with ChangeNotifier{

  MyModel _myModel=MyModel("", "", startingTime , endingTime, categoryId, userId);

  MyModel get myModel => _myModel;

  set myModel(MyModel value) {
    _myModel = value;
    notifyListeners();
  }
}

有什么建议吗,关于我应该“提供”什么?提前谢谢你。

zf9nrax1

zf9nrax11#

Declare model as below

import 'package:flutter/material.dart';

class MyModel extends ChangeNotifier {
  late String? title;
  late String? details;
  late DateTime? startingTime;

  late DateTime? endingTime;
  late int? categoryId;
  late int? userId;

  MyModel(
      {this.title,
      this.details,
      this.startingTime,
      this.endingTime,
      this.categoryId,
      this.userId});

  setValue({String? title, String? details, DateTime? startingTime,
    DateTime? endingTime, int? categoryId, int? userId}) {
    this.title = title;
    this.details = details;
    this.startingTime = startingTime;
    this.endingTime = endingTime;
    this.categoryId = categoryId;
    this.userId = userId;
    notifyListeners();
  }

  MyModel get getMyModelData {
    return MyModel(
        title: this.title,
        details: this.details,
        startingTime: this.startingTime,
        endingTime: this.endingTime,
        categoryId: this.categoryId,
        userId: this.userId);
  }
}

在您的屏幕中实现提供程序,如下所示

@override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
       
        ChangeNotifierProvider(
      
          create: (context) => MyModel(),
         
        ),
      
      ],
      child: Scaffold(
        backgroundColor: Colors.white,
        body: Center(
          child: Container(
              margin: EdgeInsets.all(15),
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    Consumer<MyModel>(builder: (newContext, _myModel, child) {
                      return Column(
                        children: [
                          Card(
                              margin: EdgeInsets.all(10),
                              elevation: 1,
                              child: (_myModel.title == null)
                                  ? Text('No Title')
                                  : Text(_myModel.title ?? '')),
                          Card(
                              margin: EdgeInsets.all(10),
                              elevation: 1,
                              child: (_myModel.details == null)
                                  ? Text('No Description')
                                  : Text(_myModel.details ?? '')),
                          Card(
                              margin: EdgeInsets.all(10),
                              elevation: 1,
                              child: (_myModel.userId == null)
                                  ? Text('No userID now')
                                  : Text((_myModel.userId ?? 0).toString())),
                          Container(
                              height: 50,
                              width: 100,
                              color: Colors.green,
                              child: MaterialButton(
                                onPressed: () {
                                  _myModel.setValue(
                                      title: 'demo title',
                                      details: 'demo des',
                                      userId: 1);
                                },
                                child: Text('Update My Model',
                                    style: TextStyle(color: Colors.white),
                                    textAlign: TextAlign.center),
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(0),
                                ),
                              ))
                        ],
                      ); // use this
                    }),
                  ],
                ),
              )),
        ),
      ),
    );
  }

相关问题