Flutter application Android store compliance processing

The Android store will require the user to clarify and authorize the application before collecting user and device information, such as mac address, imei, etc.

1. Android developers will perform this operation in the application, and the flutter application can add an authorization dialog in the main file.

Flutter's MyApp() can be regarded as the application entry of the 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);
                            },
                          ));
      }),
    );
  }
}

Add a dialog box in MyApp(), and the user clicks to agree to initialize some third-party plug-ins, such as Jiguang and Rongyun.

Judgments can be added to the body of the MyApp() build, and the authorization box will be processed first for the first installation, and the content will be initialized normally after agreeing

  @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. There will be a clickable user agreement and privacy policy in the authorization box, and the jump may encounter problems

Navigator operation requested with a context that does not include a Navigator.

This is because MyApp is a variable StatefulWidget. In this case, the above situation prompts that the routing controller needs a context but the current navigator does not contain it. Generally speaking, routing (Navigator) should be used, and the root control cannot be directly MaterialApp. Solution: Wrap  the MaterialApp content with StatelessWeight or StatefulWeight. As shown in Figure 1.

Refer to flutter error Navigator operation requested with a context that does not include a Navigator

おすすめ

転載: blog.csdn.net/LoveShadowing/article/details/123331473