Flutter使用问题整理

前言

写这篇文章是为了记录平时在使用flutter时遇到的问题,及解决方案,方便再次遇到时能够快速解决。

问题及解决

问题1

真机运行项目,在构建时提示

Running Gradle task 'assembleDebug'...                             41.8s
[!] App requires Multidex support
    Multidex support is required for your android app to build since the number of methods has exceeded 64k. You may pass the --no-multidex flag
    to skip Flutter's multidex support to use a manual solution.

解决方案:
参考:https://docs.flutter.dev/deployment/android#enabling-multidex-support

在终端上执行 flutter run --debug,当出现Do you want to continue with adding multidex support for Android? [yln]提示时,选择y

问题2
在程序中如何监听某一个变量是否发生了改变

ValueNotifier<int> count = ValueNotifier(0);

// 注册监听器
count.addListener(() {
  print("Count changed: ${count.value}");
});

// 修改变量的值
count.value = 1; // Count changed: 1

模拟请求超时
dio中可以判断是否请求超时,最近在做一个功能,需要模拟请求超时。
有时候程序一直在等待执行结果,如果执行时间比较久,就会影响到后续的执行。通过模拟请求超时,来出现长时间的等待问题。

Future<int> performTask() async {
  await Future.delayed(Duration(seconds: 3)); // 模拟一个耗时的异步操作

  return 42; // 返回执行结果
}

void main() {
  performTask().timeout(Duration(seconds: 2), onTimeout: () {
    return -1; // 在超时时返回-1
  }).then((result) {
    print('执行结果:$result');
  }).catchError((error) {
    print('发生错误:$error');
  });

 // 或者
 int res = await performTask().timeout(Duration(seconds: 2), onTimeout: () {
    return -1; // 在超时时返回-1
  }).then((result) {
    print('执行结果:$result');
    return result;
  }).catchError((error) {
    print('发生错误:$error');
  });
}

问题3

在使用Android Studio的Device Explorer 向外到处文件时提示:

Error opening contents of device file "hello.txt": Cannot create directory C:\Users\octop\Documents\AndroidStudio\DeviceExplorer\emulator-5554\sdcard\Download\hello

在这里插入图片描述
解决方案:在终端里运行 adb root
如果提示’adb’ 不是内部或外部命令,也不是可运行的程序或批处理文件”,那么就需要配置环境变量。
1、找到sdk所在的文件夹
在这里插入图片描述
在这里插入图片描述
2、在sdk目录里找到adb.exe文件
在这里插入图片描述
3、选择系统变量,在path添加adb程序所在路径
在这里插入图片描述
4、执行完adb root后,重启Android studio后可以顺利到处文件
在这里插入图片描述
问题4
在访问http开头的网络时,报错提示:

WebResourceError{description: net::ERR_CLEARTEXT_NOT_PERMITTED, type: UNKNOWN}

原因是:从Android 9(API级别28)开始,默认情况下,Android应用程序不允许加载不安全的非加密网络资源。这意味着如果你的应用程序尝试加载使用HTTP协议的资源,将会引发net::ERR_CLEARTEXT_NOT_PERMITTED错误。

解决:打开android/app/src/main/AndroidManifest.xml 文件,添加 android:usesCleartextTraffic="true" 再重新编译程序
在这里插入图片描述
问题5
有时候会遇到这样的app,在启动时需要授予权限,当拒绝授予权限后,app会自动退出。这个功能可以基于permission_handlerEasyLoading 两个插件来实现,关于这两个插件的基本使用见:

Flutter:视频下载案例

Flutter:EasyLoading(loading加载、消息提示)

代码如下,需要在main.dart里使用

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // 应用权限请求
  void requestMultiplePermissions(BuildContext context) async {
    Map<Permission, PermissionStatus> status = await [
      // 网络权限
      Permission.storage,
    ].request();
    // 判断是否存在权限未授予的情况
    bool hasPermission = status.values.every((status) => status.isGranted);
    if (!hasPermission) {
      EasyLoading.showToast("获取权限失败,应用无法正常运行");
      await Future.delayed(const Duration(seconds: 2));
      exit(0);
    }
  }

  
  Widget build(BuildContext context) {
    // 进行权限请求
    requestMultiplePermissions(context);

    return  MaterialApp(
      // 应用的主题、页面等配置
      home: MyHomePage(),
    );
  }
}
  • 16
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Podspec是CocoaPods使用的一种文件格式,用于描述一个库的元数据信息。在Flutter插件开发中,我们需要使用Podspec文件来描述iOS平台上的插件信息,以便其他开发者在使用我们的插件时能够正确地集成到他们的项目中。 下面是一些关于Podspec语法的整理: 1. Podspec文件的命名应该以插件的名称为准,并以`.podspec`作为文件扩展名,如`flutter_my_plugin.podspec`。 2. Podspec文件应该包含以下信息: - 插件的名称和版本号 - 插件的描述 - 作者信息 - 插件的许可证 - 插件依赖的库和框架 - 插件的源代码文件和资源文件 3. Podspec文件的基本结构如下: ``` Pod::Spec.new do |s| s.name = '插件名称' s.version = '插件版本号' s.summary = '插件描述' s.homepage = '插件主页' s.license = '插件许可证' s.author = { '作者名称' => '作者邮箱' } s.source = { :git => '插件源代码仓库地址', :tag => '插件版本号' } s.dependency '依赖库名称', '依赖库版本号' # 插件依赖的库和框架 s.source_files = '插件源代码文件路径' # 插件的源代码文件 s.resource_bundles = { '插件资源文件名称' => ['插件资源文件路径'] } # 插件的资源文件 end ``` 4. 在描述依赖库时,可以使用以下语法: - `s.dependency '库名称'`:插件依赖的库名称。 - `s.dependency '库名称', '~> 版本号'`:插件依赖的库名称和版本号,其中`~>`表示兼容某个版本号及以上的库。 - `s.dependency '库名称', '>= 版本号'`:插件依赖的库名称和版本号,其中`>=`表示需要某个版本号及以上的库。 5. 在描述源代码文件和资源文件时,可以使用以下语法: - `s.source_files = '路径/**/*.{h,m,swift}'`:插件的源代码文件路径,可以使用通配符`*`和`**`。 - `s.resource_bundles = { 'Bundle名称' => ['路径/**/*'] }`:插件的资源文件,可以使用通配符`*`和`**`。 以上就是关于Podspec语法的整理,希望能对Flutter插件开发有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无知的小菜鸡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值