颤动/飞镖静态变量丢失/不断重新初始化 [英] Flutter/Dart Static variables lost / keep getting reinitialized

查看:213
本文介绍了颤动/飞镖静态变量丢失/不断重新初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Flutter / Dart。但是当从另一个类访问时,我的静态变量不断重新初始化。

I am trying out things with Flutter/Dart right now. But my static variables keep getting reinitialised when accessed from another class.

我有一个类,在其单独的dart源文件中,保持服务器状态,声明如下: / p>

I have a class, in its separate dart source file, holding the server status, declared as such:

class ServerStatus{
  static int newestBinary;
  static bool serverUp;
}

我将它们初始化为@ main() by

I initialised them @ main() by

ServerStatus.newestBinary = 20;
ServerStatus.serverUp = true;

。之后,当我尝试在我的应用程序中的另一个页面上访问它们时,变量' newestBinary '和' serverUp '两者都变成了 null ,好像它们已经重新焕发活力一样。 (如果我将它们声明为 static int newestBinary = 10; ,则在<$重新分配 ServerStatus.newestBinary = 20; c $ c> main(),它仍会在我的应用程序中的另一个页面显示为10.

. Afterwards, when I try to access them at another page in my application, the variables 'newestBinary' and 'serverUp' both became null, as if they are reinitalised. (If I declare them like static int newestBinary = 10;, then reassign ServerStatus.newestBinary = 20; at main(), it would still show up as 10 at another page in my application.

我的应用程序没有退出或在两种操作之间停止。在什么情况下静态变量会被重新激活?

My application did not quit or stop between the two operations. Under what circumstances would static variables be reinitalised?

如果我必须为应用程序保存全局和常用信息,那么最好的方法是什么?不是使用静态变量吗?

If I have to hold global and commonly used information for the application, what would be the best way to do it other than using static variables?

提前致谢。

推荐答案

我玩弄了一个小时并意识到了原因。显然我做的时候:

I toyed around for an hour and realise what appears to be the reason. Apparently when I do:

import 'package:flutter_test_app/main.dart';

import 'main.dart';

即使两个源文件都属于同一个包。

Even if both source files belong to the same package.

所以最后我的测试代码如下:

So in the end my test code looks like:

main.dart:

main.dart:

import 'package:flutter/material.dart';
import 'pageA.dart';
import 'pageB.dart';
import 'pageH.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {

  static bool testFlag = false;
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {

    testFlag = true;
    ThemeData mainTheme = new ThemeData(
      primarySwatch: Colors.cyan,
    );
    print("testFlag @ MyApp: " + testFlag.toString());
    MaterialApp mainApp = new MaterialApp(
      title: 'Instabazaar',
      theme: mainTheme,
      home: new HomePage(title: 'Instabazaar'),
    );

    return mainApp;
  }
}

class HomePage extends StatefulWidget {

  final String title;
  HomePage({Key key, this.title}) : super(key: key);

  @override
  _HomePageState createState() {

    return new _HomePageState();
  }
}

class _HomePageState extends State<HomePage> {
  int _currentPageID = 0; // 0=home, 1=pageA, 2=pageB



  @override
  Widget build(BuildContext context) {

    print("testFlag @ HomePage: " + MyApp.testFlag.toString());


    AppBar appBar = new AppBar(
        title: new Text("TestApp"),
        centerTitle: true,
    );

    BottomNavigationBar bottomNavigationBar = new BottomNavigationBar(
        type: BottomNavigationBarType.shifting,
        items: <BottomNavigationBarItem>[
          new BottomNavigationBarItem(icon: new Icon(Icons.home), title: new Text('Home'), backgroundColor: Theme.of(context).accentColor),
          new BottomNavigationBarItem(icon: new Icon(Icons.explore), title: new Text('PageA'), backgroundColor: Colors.purple),
          new BottomNavigationBarItem(icon: new Icon(Icons.star), title: new Text('PageB'), backgroundColor: Colors.redAccent),
        ],
        onTap: (i) => setState( () => _currentPageID = i ),
        currentIndex: _currentPageID
    );


    Scaffold mainScaffold = new Scaffold(
      appBar: appBar,
      body: _getNewSubPage(),
      bottomNavigationBar: bottomNavigationBar,
    );
    return mainScaffold;
  }


  //MARK: navigation


  Widget _getNewSubPage(){
    switch (_currentPageID)
    {
      case 1:
        return new pageA();
      case 2:
        return new pageB();
      default:
        return new pageH();
    }
  }


}

pageA.dart / pageB.dart:

pageA.dart / pageB.dart:

import 'package:flutter/material.dart';
import 'package:flutter_test_app/main.dart';

class pageA extends StatefulWidget{
  pageAState createState() => new pageAState();
}


class pageAState extends State<pageA> {

  @override
  Widget build(BuildContext context) {
    print("testFlag @ pageA: " + MyApp.testFlag.toString());
    return new Container();
  }
}

pageH.dart:

pageH.dart:

import 'package:flutter/material.dart';
import 'main.dart';

class pageH extends StatefulWidget{
  pageHState createState() => new pageHState();
}


class pageHState extends State<pageH> {
  @override
  Widget build(BuildContext context) {
    print("testFlag @ pageH: " + MyApp.testFlag.toString());
    return new Container();
  }
}

唯一的区别是import语句。但是,对于pageA / pageB,print语句将给出false。至于pageH,print语句将给出true。我已经切换了import语句并检查出来。我不熟悉dart实际上如何解释代码,所以我不确定它是一个飞镖的东西,一个设置的东西或一个颤动的东西。我会继续调查,但现在我的问题已经解决了。

The only difference is the import statement. However, for pageA/pageB, the print statement would give "false". As for pageH, the print statement would give "true". I have switched around the import statements and it checks out. I am not familiar with how dart actually interprets the code, so I am not sure if it is a dart thing, a setup thing or a flutter thing. I will continue investigating but for now my problem is solved.

感谢大家的帮助。

这篇关于颤动/飞镖静态变量丢失/不断重新初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆