簡體   English   中英

Flutter 將值傳遞給全局變量

[英]Flutter pass a value to a global variable

我第一次使用 flutter 和 stackoverflow,請原諒我的錯誤。 我在 global.dart 文件中有全局變量。 在我的主要內容中,我想通過傳遞值來訪問和編輯變量

Global.dart
String nome_impianto1 = "IMPIANTO 1";
String nome_impianto2 = "IMPIANTO 2";
String nome_impianto3 = "IMPIANTO 3";
String nome_impianto4 = "IMPIANTO 4";

main.dart
import 'package:services/global.dart' as globals;
globals.nome_impianto1 = usrcontroller_ovrelay_impianto.text;  //so it works.

如何傳遞我想要更改的值?

globals.nome_impianto{$'1'} = usrcontroller_ovrelay_impianto.text //I know it doesn't work, and just to get the idea;

感謝所有最好的問候安德里亞

將此添加到您的任何 function 中:

 setState((){
      globals.nome_impianto1 = usrcontroller_ovrelay_impianto.text;
    });
  1. 避免在 flutter 中使用全局變量。 如果您正在管理 state 全局使用StatefulWidget ,甚至更好地開始使用provider甚至更高級的bloc

  2. 對於您的問題,如果您想在全球范圍內訪問這些內容,最好使用Map

//  Global.dart
  Map<String, String> globals = {
    "nome_impianto1": "IMPIANTO 1",
    "nome_impianto2": "IMPIANTO 2",
    "nome_impianto3": "IMPIANTO 3",
    "nome_impianto4": "IMPIANTO 4",
  };

// main.dart
  int index = 1;
  globals["nome_impianto$index"] = usrcontroller_ovrelay_impianto.text;
  1. flutter 沒有反射https://stackoverflow.com/a/49871692/8608146

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM