Flutter 添加 example流程

一、已有Flutter工程(命令)添加 example

1、cd 工程(flutter_plugin ,是自己创建的)根目录

      例: flutter create example  执行命令创建example


PS:cd example 后执行flutter doctor 后就可以看到效果

2、如果需要指定iOS/Android 语言,请添加 "-i  / -a" 参数

     例: flutter create -i objc -a kotlin example

    PS:cd example 后执行flutter doctor 后就可以看到效果

3、运行效果图:

二、已有Flutter工程(图形)添加 example

以下结构中,我们重点关注其中4个文件夹:
1.android(完整的 Android 工程,用来开发 Android 端的插件功能)
2.ios(完整的 IOS工程,用来开发 IOS端的插件功能)
3.example(用来测试 Android 或者 IOS 端的插件功能)
4.lib(负责和 Android 或者 IOS 端的交互)

谢谢大家的关注🙏 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这里是一个简单的Flutter MQTT例子,它连接到一个公共MQTT代理,并订阅一个主题。当收到来自该主题的消息时,它会在UI上显示它们。 首先,您需要在`pubspec.yaml`文件中添加`mqtt_client`依赖项,然后运行`flutter packages get`以安装它。 ``` dependencies: mqtt_client: ^4.0.0 ``` 然后,您可以使用以下代码创Flutter应用程序。 ``` import 'package:flutter/material.dart'; import 'package:mqtt_client/mqtt_client.dart'; import 'package:mqtt_client/mqtt_server_client.dart'; void main() { runApp(MyApp()); } class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { MqttServerClient client; String message = ''; @override void initState() { super.initState(); client = MqttServerClient('broker.hivemq.com', ''); client.onConnected = onConnected; client.onSubscribed = onSubscribed; client.onDisconnected = onDisconnected; client.onUnsubscribed = onUnsubscribed; client.pongCallback = pong; connect(); } void connect() async { try { await client.connect(); } catch (e) { print('Exception: $e'); } } void subscribe() { client.subscribe('topic/test', MqttQos.atLeastOnce); } void onConnected() { print('Connected'); subscribe(); } void onSubscribed(String topic) { print('Subscribed to $topic'); } void onDisconnected() { print('Disconnected'); } void onUnsubscribed(String topic) { print('Unsubscribed from $topic'); } void pong() { print('Ping response client callback invoked'); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('MQTT Chat'), ), body: Center( child: Text(message), ), ), ); } } ``` 在上面的代码中,我们首先创了一个`MqttServerClient`对象,它连接到公共MQTT代理`broker.hivemq.com`。我们还设置了一些回调函数,以便在连接,订阅和断开连接时得到通知。 在`initState`方法中,我们连接到MQTT服务器。一旦连接成功,我们将订阅`topic/test`主题。 当我们收到来自该主题的消息时,我们将在UI上显示它们。为此,我们可以在`onSubscribed`回调函数中添加以下代码。 ``` void onSubscribed(String topic) { print('Subscribed to $topic'); client.updates.listen((List<MqttReceivedMessage<MqttMessage>> messages) { final recMess = messages[0].payload as MqttPublishMessage; final pt = MqttPublishPayload.bytesToStringAsString(recMess.payload.message); setState(() { message = pt; }); }); } ``` 上面的代码将使用`client.updates`流监听来自MQTT服务器的消息。当收到消息时,它将在UI上显示它们。 这是一个简单的Flutter MQTT例子。您可以根据自己的需要进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值