安卓商店在应用上架时会要求用户清楚并授权之后才可收集用户和设备信息,如mac地址 imei等
1、安卓开发者会在application中进行此操作,flutter应用可在main文件中添加授权对话框,
flutter的MyApp()可等同看作是应用的application入口。
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
///等同于应用的application
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage());
}
}
///为了解决路由跳转问题
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("route"),
),
floatingActionButton: FloatingActionButton(onPressed: (){
Navigator.push(contexts,MaterialPageRoute(
builder: (context) {
return AgreementPage(1);
},
));
}),
);
}
}
在MyApp()中添加对话框,用户点击同意即进行部分三方插件的初始化,如极光、融云等。
可在MyApp()的build的body中添加判断,初次安装先处理授权框,同意后再正常初始化内容
@override
Widget build(BuildContext context) {
appContext = context;
return FutureBuilder<CacheManager>(
future: CacheManager.preInit(context),//进行初始化
builder: (BuildContext context, AsyncSnapshot<CacheManager> snapshot) {
//定义route
var _widget = (snapshot.connectionState == ConnectionState.done &&
snapshot.data != null)
? Router(routerDelegate: _routeDelegate)
: snapshot.connectionState != ConnectionState.done
? Scaffold(
body: Center(
child: CircularProgressIndicator(color: ColorTool.red),
),
)
:_showAgreementWindow(appContext);
return _widget;
},
);
}
2、授权框中会有可点击的用户协议和隐私政策,跳转可能会遇到问题
Navigator operation requested with a context that does not include a Navigator.
这是由于MyApp 是一个可变StatefulWidget这种情况下,如上情况提示路由控制器需要一个context但是当前navigator并不包含,通俗的讲要使用路由(Navigator),根控件不能直接是 MaterialApp.解决方法:将 MaterialApp 内容再使用 StatelessWeight 或 StatefulWeight 包裹一层。如图1所示。
可参考flutter报错Navigator operation requested with a context that does not include a Navigator