Flutter应用调试后持续崩溃求助,附日志与相关代码
Flutter调试后崩溃,卡在构建成功界面
问题现象
调试启动应用后直接崩溃,终端仅停留在以下构建成功的日志:
Launching lib\main.dart on sdk gphone x86 arm in debug mode... lib\main.dart:1 √ Built build\app\outputs\flutter-apk\app-debug.apk.
(用户提供对应崩溃截图)
当前正在通过Dio连接Laravel后台API,相关Dio工具类代码如下:
import 'package:dio/dio.dart'; class BaseUrl { static String url = 'https://awad.mohatim.tech/public/api'; static String real = 'https://awad.mohatim.tech/api/'; static String local = 'http://127.0.0.1:8000/api/'; } Map<String, String> headers = { 'Accept': 'application/json', 'Content-Type': 'application/json', }; class DioHelper { static final BaseOptions _options = BaseOptions( baseUrl: BaseUrl.real, headers: headers, validateStatus: (status) { return status! < 500; }, ); static final Dio _dio = Dio(_options); // get login # data = email and password static Future getLogin({required Map<String, dynamic> data}) async { try { Response response = await _dio.post('login', data: data); return response; } on DioError catch (e) { print(e.message); } } // data = name email phone paswword static Future register({required Map<String, dynamic> data}) async { try { Response response = await _dio.post('register', data: data); return response; } on DioError catch (e) { print(e.message); } } // public request static Future post({required String endpoint, required dynamic data}) async { Map<String, String> headers = { 'Accept': 'application/json', 'Content-Type': 'application/json', }; try { Response response = await _dio.post(endpoint, data: data, options: Options(headers: headers)); return response; } on DioError catch (e) { print(e.message); } } // public request static Future get({required String endpoint}) async { try { Response response = await _dio.get(endpoint); return response; } on DioError catch (e) { print(e.message); } } static Future getWithToken({required String endpoint, required String token}) async { Map<String, String> headers = { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': 'Bearer $token', }; try { Response response = await _dio.get(endpoint, options: Options(headers: headers)); return response; } on DioError catch (e) { print(e.message); } } static Future postWithToken({required String endpoint, required dynamic data, required String token}) async { Map<String, String> headers = { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': 'Bearer $token', }; try { Response response = await _dio.post(endpoint, data: data, options: Options(headers: headers)); return response; } on DioError catch (e) { print(e.message); } } static Future postImage({required String endpoint, required FormData data, required String token}) async { Map<String, String> headers = { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': 'Bearer $token', }; try { Response response = await _dio.post(endpoint, data: data, options: Options(headers: headers)); return response; } on DioError catch (e) { print(e.message); } } }
排查与解决建议
- 获取完整崩溃日志:别只看终端构建日志,打开Android Studio的
Logcat面板,过滤Error/Fatal级别日志,找到具体的崩溃堆栈,这是定位问题的关键。iOS模拟器则查看Xcode控制台输出。 - 修复BaseUrl格式问题:
BaseUrl.real末尾带斜杠,请求的endpoint(如login)无前缀斜杠,拼接后会出现//,导致请求地址错误。统一格式:要么BaseUrl末尾不带斜杠,要么endpoint开头加斜杠。 - 处理未捕获异常:当前Dio方法仅捕获
DioError并打印日志,请求失败时返回null,后续逻辑若未处理空值会引发空指针崩溃。建议在catch块抛出异常或返回明确错误对象,调用异步方法时用try-catch包裹。 - 修正postImage的Content-Type:上传
FormData时,手动设置application/json会导致格式错误,Dio会自动生成multipart/form-data头,移除该方法中的Content-Type配置。 - 检查网络权限与SSL:确认AndroidManifest.xml添加了
<uses-permission android:name="android.permission.INTERNET"/>;真机调试HTTPS证书不被信任时,可临时在Dio中添加证书忽略(仅调试用):static final BaseOptions _options = BaseOptions( baseUrl: BaseUrl.real, headers: headers, validateStatus: (status) { return status! < 500; }, httpClientAdapter: HttpClientAdapter()..onHttpClientCreate = (client) { client.badCertificateCallback = (cert, host, port) => true; return client; }, ); - 确认初始化时机:确保DioHelper在应用启动后再被调用,避免未初始化时发起请求。
内容的提问来源于stack exchange,提问作者Mr Wolf
相关产品推荐
相关产品推荐

