在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/