![](/img/trans.png)
[英]How to pass context or any parameter in a static class in flutter?
[英]how to pass context outside stless and staful class flutter
我想在常量文件中使用上下文,例如
class StatusCodes {
static Map<int, String> overTimeCancelResponse = {
0: "OverTime request Cancel Successfully",
1: "Error Cancelling OverTime Request",
};
}
or
class StatusCodes {
static Map<int, OverTimeResponses> overTimeResponse = {
0: OverTimeResponses(
"Overtime Request Submitted Successfully",
Colours.green,
),
};
}
我正在使用語言文件將 eng 更改為像這樣的不同語言,例如
class StatusCodes {
static Map<int, String> overTimeCancelResponse = {
0: S.0f(context).cancel_request,
1: S.0f(context).error,
};
}
但我無法在這里使用上下文
您可以創建一個 Map function function,通過參數傳遞上下文並返回您的 Z1D78DC15ED5141214E5AEZ22
請參閱下面的代碼
class StatusCodes {
Map<int, String> overTimeCancelResponse(BuildContext context) {
return {
0: "OverTime request Cancel Successfully", // you can pass a value with context here
1: "Error Cancelling OverTime Request",
};
}
}
也許你不應該,但如果你“必須”這是你能得到的最接近的:
class StatusCodes {
static Map<int, String> getOverTimeCancelResponse(BuildContext context) {
// Do something with context
// ....
// build your new "overTimeCancelResponse"
return newOverTimeCancelResponse;
}
}
不能在 static 變量中使用BuildContext
(或任何其他變量)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.