如何将Native java中的变量值获取到Flutter



我的应用程序从蓝牙接收数据,并将其存储在Java(Native(的变量中。我可以在toast消息中显示。需要移动值以颤动我必须在initstate中显示flutter中的值。有人能帮我吗?我的管家。

handler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(Message msg){
if(msg.what == MESSAGE_READ){
readMessage = null;
try {
readMessage = new String((byte[]) msg.obj, "UTF-8");

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(),"MEssage"+readMessage,Toast.LENGTH_LONG).show();
}
if(msg.what == CONNECTION_STATUS){
if(msg.arg1 == 1)
{
bluetoothStatus=true;
result.success(bluetoothStatus);
}
else
{
bluetoothStatus=false;
result.success(bluetoothStatus);
}
}
}
};

官方文档中有一个这样做的方法(https://flutter.dev/docs/development/platform-integration/platform-channels)基本上,您需要用Java编写一个方法,并在Flutter中按名称调用它;因为它是异步的,所以你可以使用像这样的

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
final Future<String> _calculation = Future<String>.delayed(
const Duration(seconds: 2),
() => 'Data Loaded',
);
@override
Widget build(BuildContext context) {
return DefaultTextStyle(
style: Theme.of(context).textTheme.headline2!,
textAlign: TextAlign.center,
child: FutureBuilder<String>(
future: _calculation, 
.........

最新更新