简体   繁体   English

Flutter Riverpod 在没有上下文的情况下设置值

[英]Flutter Riverpod Setting a Value Without Context

I have an app where a video uploading progress indicator needs to be shown on the Feed Screen.我有一个应用程序,其中需要在 Feed 屏幕上显示视频上传进度指示器。 The code that returns the upload progress is in a Video Client Class that is not in the widget tree of the Feed Screen and the Feed Screen is not even open at the time the video starts uploading.返回上传进度的代码位于视频客户端 Class 中,它不在 Feed 屏幕的小部件树中,并且在视频开始上传时 Feed 屏幕甚至没有打开。

What I am trying to do is use Riverpod to update the upload value globally in the Video Client Class so when the Feed Screen is opened it shows the upload progress.我想要做的是使用 Riverpod 在视频客户端 Class 中全局更新上传值,因此当打开提要屏幕时,它会显示上传进度。

The problem is the Video Client is a seperate class without any build context and I am trying to update the value.问题是视频客户端是一个单独的 class 没有任何构建上下文,我正在尝试更新值。 I have heard Riverpod can set state without build context.我听说 Riverpod 可以在没有构建上下文的情况下设置 state。 But the only way I can see to update the state still requires context.但我能看到更新 state 的唯一方法仍然需要上下文。

In the main function I add the provider scope here:在主要的 function 中,我在此处添加提供程序 scope:

void main() {
  FlowRouter.setupRouter();
  runApp(ProviderScope(child: MyApp()));
}

I have a upload model that extends change notifier for updating the values:我有一个上传 model 扩展更改通知以更新值:

class UploadModel extends ChangeNotifier{

  UploadModel();

  int _bytesTotal = 0;
  int _bytesUploaded = 0;
  bool _uploadVisible = false;
  
  void setBytesTotal(int value){
    _bytesTotal = value;
    notifyListeners();
  }
  int get getBytesTotal => _bytesTotal;
  
  void setBytesUploaded(int value){
    _bytesUploaded = value;
    notifyListeners();
  }
  int get getBytesUploaded => _bytesUploaded;
  
  void setUploadVisible(bool value){
    _uploadVisible = value;
    notifyListeners();
  }
  bool get getUploadVisible => _uploadVisible;
}

In the Feed Widget I have the following code to watch for and update the upload widget:在 Feed 小部件中,我有以下代码来监视和更新上传小部件:

final uploadProvider = riverpod.ChangeNotifierProvider((ref) => UploadModel());

class  UploaderProgressWheel extends riverpod.ConsumerWidget {
  @override
  Widget build(BuildContext context, ScopedReader watch) {

    final uploadInfo = watch(uploadProvider);

    return Text(uploadInfo.getBytesUploaded.toString());
}

But then when I go to update the the upload values in the Video Client Class I can't see any way to do it without needing context, I think I need to add the provider in in the video client class again like this:但是当我 go 更新视频客户端 Class 中的上传值时,如果不需要上下文,我看不到任何方法,我想我需要在视频客户端 ZA2F2ED4F8EBC2CBB4C21A29DDC40AB6 中再次添加提供程序:

final uploadProvider = riverpod.ChangeNotifierProvider((ref) => UploadModel());

then I want to access set the value with setBytesUploaded() from the UploadModel class but I have no access to it and the only examples I have found are like below which requires context:然后我想从 UploadModel class 中使用 setBytesUploaded() 访问设置值,但我无法访问它,我发现的唯一示例如下所示,需要上下文:

context.read(uploadProvider).state = 

Thanks谢谢

class VideoClient { //Example class
   final Reader read; //so you can read other providers inside this
   int _i = 0; //just for example

   VideoClient(this.read);

   void exampleMethod(){
     read(uploadProvider).setBytesUploaded(_i++);
     // each time this method is called a new int is sent to notify the uploadModel
   }

   //.... your methods
}

final VideoProvider = Provider((ref) => VideoClient(ref.read)); // to have a single instance of your VideoClient across all your app

And now when you want to upload a video or do something specific with your VideoClient you call this provider from anywhere in your app现在,当您想上传视频或使用 VideoClient 执行特定操作时,您可以从应用程序中的任何位置调用此提供程序

RaisedButton(
  onTap: () {
    context.read(VideoProvider).upload(); //or whatever method you use to initilize an action
  }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM