Flutter/Dart 变量丢失/尝试从不同 class 获取时不断重新初始化
Flutter/Dart variables lost / keep getting reinitialized when trying to get from different class
当从另一个 class 访问时,变量不断重新初始化。
我有一个 class,在其单独的飞镖源文件中,声明如下:
helper_provider.dart
import 'package:flutter/material.dart';
class BuilderHelperProvider extends ChangeNotifier {
var _builderDetails;
void setBuilderDetails(inputDetails) {
this._builderDetails = inputDetails;
}
get builderDetails {
return _builderDetails;
}
}
然后我在main.dart
中初始化变量
main.dart
import 'package:builder_project/helpers/helper_provider.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(
create: (context) => BuilderHelperProvider(),
),
],
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: StreamBuilder(
stream: FirebaseFirestore.instance
.collection('builders')
.doc('builder id')
.snapshots(),
builder: (context, builderSnap) {
if (builderSnap.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
}
final _builderDetails = builderSnap.data;
BuilderHelperProvider().setBuilderDetails(_builderDetails);
print('builder Details: ${BuilderHelperProvider().builderDetails}');
# *This keeps returning null*
return BuilderHomepage();
})
// home: BuilderHomepage(),
),
);
}
}
以上代码的输出结果为:
I/flutter (31260): builder Details: null
我的应用程序在两次操作之间没有退出或停止。什么情况下会重新激活?
只需将它们设为静态就可以了
static var _builderDetails;
当从另一个 class 访问时,变量不断重新初始化。
我有一个 class,在其单独的飞镖源文件中,声明如下:
helper_provider.dart
import 'package:flutter/material.dart';
class BuilderHelperProvider extends ChangeNotifier {
var _builderDetails;
void setBuilderDetails(inputDetails) {
this._builderDetails = inputDetails;
}
get builderDetails {
return _builderDetails;
}
}
然后我在main.dart
中初始化变量main.dart
import 'package:builder_project/helpers/helper_provider.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(
create: (context) => BuilderHelperProvider(),
),
],
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: StreamBuilder(
stream: FirebaseFirestore.instance
.collection('builders')
.doc('builder id')
.snapshots(),
builder: (context, builderSnap) {
if (builderSnap.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
}
final _builderDetails = builderSnap.data;
BuilderHelperProvider().setBuilderDetails(_builderDetails);
print('builder Details: ${BuilderHelperProvider().builderDetails}');
# *This keeps returning null*
return BuilderHomepage();
})
// home: BuilderHomepage(),
),
);
}
}
以上代码的输出结果为:
I/flutter (31260): builder Details: null
我的应用程序在两次操作之间没有退出或停止。什么情况下会重新激活?
只需将它们设为静态就可以了
static var _builderDetails;