Flutter入门开发记录

这篇博客记录了Flutter入门开发中遇到的问题及解决方案,包括环境安装、依赖包管理、API更新问题以及如何修复pub缓存错误。通过修改pubspec.yaml文件、使用flutter pub get命令、更新代码以适配新版本API等方法,解决了开发过程中的各种障碍。
摘要由CSDN通过智能技术生成

Flutter入门开发记录

一、了解Flutter

中文官网 flutterchina.club

二、安装环境

安装 https://flutterchina.club/get-started/install/
windows https://flutterchina.club/setup-windows/
使用镜像可以执行以下语句,windows可以在环境变量中配置

export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

flutter必须要先安装对应sdk,推荐安装稳定版的最新版本
安装位置一般放在C盘或者D盘根目录下
例如D:/src/flutter

三、常见问题

导入新项目Target of URI doesn’t exist ‘package:flutter/material.dart’

命令行里运行 flutter packages get去获取新的包

获取新包错误Because no versions of charts_flutter match >0.8.1 <0.9.0 and

这是因为当时版本的包与新版的flutter不兼容导致。(你最近应该是升级了flutter,或者与原项目的flutter版本不一致)
如果确信这个依赖包有对应最新flutter的版本,则可以修改其依赖版本为any,然后运行flutter packages get
修改方法:
打开文件pubspec.yaml,在依赖dependencies中找到charts_flutter,版本修改为any
之后查看pubspec.lock文件中flutter自动拉去的不冲突的版本,将版本号复制到pubspec.yaml中。

pub get failed (66; Could not decompress gz stream Connection closed while receiving data, path = ‘’)

运行 flutter pub pub cache repair

Failed to reinstall 17 packages:
- _fe_analyzer_shared 12.0.0
- analyzer 0.40.6
- dart_style 1.3.10
- dio 2.2.2
- extended_text 0.6.9
- file 6.0.0
- test 1.16.0
- url_launcher 5.7.10
- url_launcher 6.0.0-nullsafety.1
- url_launcher_macos 0.0.1+9
- vector_math 2.1.0
- video_player 2.0.0-nullsafety.2
- vm_service 5.5.0
- win32 2.0.5
- analyzer 0.40.6
- js 0.6.3
- vm_service 5.5.0
pub finished with exit code 69

再次运行flutter pub pub cache repair,报错
Could not find a file named “pubspec.yaml” in “D:\src\flutter.pub-cache\hosted\pub.flutter-io.cn\js-0.6.3”.
运行flutter packages get也能看见类似的错误
删除对应的空文件夹
再次运行flutter pub pub cache repair,直到修复。。。。过程中出错,多半因为网络不好引起。

最终删除原有cache目录D:\src\flutter.pub-cache\下所有资源,然后使用android studio打开pubspec.yaml
点击pub get / pub upgrade来解决问题。

Flutter 1.12更新后部分API修改问题

The method ‘inheritFromWidgetOfExactType’ isn’t defined for the type ‘BuildContext’.
https://blog.csdn.net/qq_24724109/article/details/104619670

  static InheritedStates of(BuildContext context) {
    return context.inheritFromWidgetOfExactType(InheritedStates) as InheritedStates;
  }

改为

  static InheritedStates of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<InheritedStates>();
  }
The method ‘ancestorWidgetOfExactType’ isn’t defined for the type ‘BuildContext’.

This error is caused by inheritFromWidgetOfExactType method.
This method is deprecated . Use dependOnInheritedWidgetOfExactType instead.

原有代码

(context.inheritFromWidgetOfExactType(BlocProvider) as BlocProvider);

改为

(context.dependOnInheritedWidgetOfExactType<BlocProvider>());
ancestorWidgetOfExactType报错找不到方法

修改为findAncestorWidgetOfExactType

	// final type = _typeOf<BlocProvider<T>>();
    // BlocProvider<T> provider = context.ancestorWidgetOfExactType(type);
    BlocProvider<T> provider = context.findAncestorWidgetOfExactType<BlocProvider>();
pub upgrade失败

https://www.yuque.com/wiszsw/wtdm6g/hfbig6
删除cache 重启电脑

flutter依赖包版本锁定

https://blog.csdn.net/mqdxiaoxiao/article/details/102515944

No named parameter with the name shadowThemeOnly

https://blog.csdn.net/weixin_38265081/article/details/114539827
新版 flutter shadowThemeOnly 主题被移除
注释掉这个参数就行

class xxx extend Diagnosticable 报错

查看Diagnosticable的定义,属于mixin。
报错的意思类只能继承类,而mixin不是class,所以只能with
class DatePickerTheme with Diagnosticable

Flutter 常用工具类库

https://blog.csdn.net/aau88497/article/details/102344984

Dart常用工具类库 common_utils
Flutter工具类库 flustars

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值