1

我试图在 Android TV Box 上运行我的应用程序,但是每当我发出一个简单的 HTTP 请求(使用 DIO 包或 HTTP 包)时,我的应用程序就会突然强制关闭。即使我使用 Catcher 包来全局捕获我的错误,它也没有抛出任何错误。

我尝试在几种 Android Box 设备上运行我的应用程序,例如 STB ZTE B860H、STB ZTE B760H、STB HUAWEI HG680,但它仍然崩溃。但是,当我尝试在移动设备或模拟器上运行该应用程序时,它运行良好。

这是我的代码,每当我点击按钮时。我的应用程序突然强制关闭。

主要飞镖

  void main() {
  CatcherOptions debugOptions =
      CatcherOptions(DialogReportMode(), [ConsoleHandler()]);
  CatcherOptions releaseOptions = CatcherOptions(DialogReportMode(), [
    EmailManualHandler(['SomeEmail@Email.com'],
        enableApplicationParameters: true,
        enableDeviceParameters: true,
        enableStackTrace: true)
  ]);
  CatcherOptions profileOptions = CatcherOptions(
      DialogReportMode(), [ConsoleHandler(), ToastHandler()],
      handlerTimeout: 1000);
  Catcher(MyApp(),
      debugConfig: debugOptions,
      releaseConfig: releaseOptions,
      profileConfig: profileOptions,
      enableLogger: true);
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeLeft,
      DeviceOrientation.landscapeRight,
    ]);

    return MaterialApp(
      navigatorKey: Catcher.navigatorKey,
      theme: ThemeData.dark(),
      home: MultiProvider(
        providers: [
          ChangeNotifierProvider<UsersProvider>(
            create: (_) => UsersProvider(),
          ),
        ],
        child: EntryPage(),
      ),
    );
  }
}

入口页面.dart

class EntryPage extends StatefulWidget {
  EntryPage({Key key}) : super(key: key);

  @override
  _EntryPageState createState() => _EntryPageState();
}

class _EntryPageState extends State<EntryPage> {
  TextEditingController _codeTextEditingController = TextEditingController();
  MaskTextInputFormatter maskFormatter = new MaskTextInputFormatter(
      mask: '#### ####', filter: {"#": RegExp(r'[0-9]')});

  @override
  Widget build(BuildContext context) {
    final usersProvider = Provider.of<UsersProvider>(context);
    final bloc = usersProvider.usersBloc;

    SystemChrome.setEnabledSystemUIOverlays([]);
    SizeConfig().init(context);
    return Scaffold(
      body: Container(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Hero(
                tag: 'logo',
                child: Image(
                  image: AssetImage('assets/LOGO.png'),
                  alignment: Alignment.center,
                  height: SizeConfig.screenHeight * 0.30,
                ),
              ),
              SizedBox(
                height: SizeConfig.blockSizeVertical * 5,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  SizedBox(
                    height: SizeConfig.appBarHeight,
                    width: SizeConfig.screenWidth * 0.5,
                    child: StreamBuilder<String>(
                        stream: bloc.code,
                        builder: (context, snapshot) {
                          return TextField(
                            autofocus: true,
                            inputFormatters: [maskFormatter],
                            controller: _codeTextEditingController,
                            onChanged: bloc.changeCode,
                            keyboardType: TextInputType.number,
                            decoration: InputDecoration(
                                focusedBorder: OutlineInputBorder(
                                    borderRadius: BorderRadius.circular(10),
                                    borderSide: BorderSide(
                                        color: snapshot.hasError
                                            ? Colors.red
                                            : Colors.tealAccent[400])),
                                border: OutlineInputBorder(
                                  borderRadius: BorderRadius.circular(10),
                                ),
                                prefixIcon: Icon(Icons.playlist_add),
                                labelStyle: TextStyle(
                                    color: snapshot.hasError
                                        ? Colors.red
                                        : Colors.tealAccent[400]),
                                labelText: "Enter Your Code Here"),
                          );
                        }),
                  ),
                  SizedBox(
                    width: SizeConfig.blockSizeHorizontal,
                  ),
                  StreamBuilder<String>(
                    stream: bloc.code,
                    builder: (context, snapshot) {
                      return Container(
                        height: SizeConfig.appBarHeight,
                        child: RaisedButton(
                          shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(10)),
                          onPressed: snapshot.hasError
                              ? null
                              : () {
                                  validate(context);
                                },
                          child: Text('Play'),
                        ),
                      );
                    },
                  )
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  void validate(BuildContext context) async {
    //Get String
    String code = maskFormatter.getUnmaskedText();
    //Provider
    final usersProvider = Provider.of<UsersProvider>(context, listen: false);
    //Call the server
    Response response = await Dio().get(
        'https://SomeLink.com',
        options: Options(
            followRedirects: false,
            validateStatus: (status) {
              return status < 500;
            }));
    //IF OK then
    if (response.statusCode == 200) {
      usersProvider.setCode(code);
    } else {
      Scaffold.of(context).showSnackBar(SnackBar(
        backgroundColor: Colors.red,
        content: Text(
          'Your Code is Invalid',
          style: TextStyle(color: Colors.white),
        ),
        duration: Duration(seconds: 1),
      ));
    }
  }

  @override
  void dispose() {
    _codeTextEditingController.dispose();
    super.dispose();
  }
}

用户块.dart

class UsersBloc {
  //Declare Streams
  final _code = BehaviorSubject<String>();

  //Getter
  Stream<String> get code => _code.stream.transform(validateCode);

  //Setter
  Function(String) get changeCode => _code.sink.add;

  //Validators
  final validateCode = StreamTransformer<String, String>.fromHandlers(
    handleData: (data, sink) {
      String code = data.replaceAll(' ', '');
      if (code.length == 8) {
        sink.add(code);
      } else {
        sink.addError('Code must be 8 digits');
      }
    },
  );

  //Dispose The Stream
  dispose() {
    _code.close();
  }
}
4

0 回答 0