简体   繁体   English

如何在 flutter dart 中访问提供程序中的 getter

[英]How to access the getter in provider in flutter dart

I have assigned the value from changeNotifierproxyProvider to the Company provider.我已将 changeNotifierproxyProvider 的值分配给 Company 提供程序。 I have to access the value from the Company class provider to screen widget.我必须从公司 class 提供程序访问屏幕小部件的值。

My code is我的代码是

Company.dart公司.dart

class Company with ChangeNotifier{
final bool _companyCreated;

Company(this._companyCreated);

get isCompanyCreated{
return _companyCreated;
}

ProducerSignUpScreen.dart制片人SignUpScreen.dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:scopemobileapp/providers/company.dart';

class ProducerSignUpScreen extends StatefulWidget {
static const routeName = '/producer-signup';

}

@override
_ProducerSignUpScreenState createState() => _ProducerSignUpScreenState();
}

class _ProducerSignUpScreenState extends State<ProducerSignUpScreen> {
**bool companycreated = Company.isCompanyCreated();**
}

i want to call the isCompanyCreated from the producer sign up screen and assign the value to variable companyCreated我想从生产者注册屏幕调用 isCompanyCreated 并将值分配给变量 companyCreated

I guess you can use Provider.of<Company>(context);我猜你可以使用Provider.of<Company>(context); in your build method so you can have access to an instance of Company class.在您的build方法中,以便您可以访问Company class 的实例。 Also, don't forget to provide Company class in the nearest parent of the children that use Provider.of(context) so InheritedWidget can access the instance you want to use.此外,不要忘记在使用Provider.of(context)的子代最近的父代中提供Company class,以便InheritedWidget可以访问您要使用的实例。

Widget build(BuildContext context){
      final company = Provider.of<Company>(context);
      bool companycreated = company.isCompanyCreated;
      return .....

} }

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

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