flutter - 替换dart中的变量

标签 flutter http dart

在consts类中,我已经声明了
常量

static const URL = "";
在登录块中,成功登录后,服务器将把url返回给我,我将url分配给此变量。
Response user =
          await _repo.getLogin(context, email, password);
      var baseResponse = UserResponse.fromJson(user.body);
      if (baseResponse.status == 101) {
        Consts.URL =  baseResponse.url;
       }
在repo类中,假设我有5种方法,我需要在consts类中使用url,这是一个好的解决方案并且可能吗?
Future create(){
    try{
     var request = http.MultipartRequest('POST',Uri.parse(Consts.URL));
    }catch(e){
    }
}

最佳答案

您不能在运行时为const赋值,因为const是编译时常量。
请引用有关https://dart.dev/guides/language/language-tour#final-and-const的文档

Final and const If you never intend to change a variable, use final or const, either instead of var or in addition to a type. A final variable can be set only once; a const variable is a compile-time constant. (Const variables are implicitly final.) A final top-level or class variable is initialized the first time it’s used.

关于flutter - 替换dart中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64765088/

相关文章:

dart - Flutter 测试 MissingPluginException

php - 如何使用 Android 中的 HTTP 将 Android 中 SQLite 数据库中存储的数据发送到 PHP Web 服务器

http - 您将如何在现代网站上使用 PUT 或 DELETE HTTP 请求

flutter - 首次启动时启动欢迎屏幕

dart - 是否可以修改 Dart 中参数的引用?

ios - Flutter - iOS 模拟器 "not supported devices connected"

firebase - Flutter/Firebase getter 'length'在null上被调用

c - 返回 301 永久移动的 UNIX C HTTP 请求

ios - 如何调用原生IOS中的Method向Flutter发送数据?

Flutter 更改主题中所有凸起按钮的文本颜色