Flutter App Suddenly Force Closed Whenever I Make an HTTP Request on Android TV Box

497 Views Asked by At

I tried to run My App on Android TV Box, but whenever i make a simple HTTP Request (either using DIO package or HTTP package) my app suddenly force closed. it didn't throw any error even tho i'm using Catcher package to catch my error globally.

I've tried to run my app on several kind of Android Box Devices such as STB ZTE B860H, STB ZTE B760H, STB HUAWEI HG680, and it still crashes. But, when i tried to run the app on Mobile Devices or Emulators, it works perfectly.

here's my code, whenever i clicked on the button. My app suddenly force closed.

Main.Dart

  void main() {
  CatcherOptions debugOptions =
      CatcherOptions(DialogReportMode(), [ConsoleHandler()]);
  CatcherOptions releaseOptions = CatcherOptions(DialogReportMode(), [
    EmailManualHandler(['[email protected]'],
        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(),
      ),
    );
  }
}

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();
  }
}

UsersBloc.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();
  }
}
0

There are 0 best solutions below