0

Very simple login form, works on iOS and web, but not on Android. Is there something fundamental, or is there some bug related to this. Basically, clicking on the "submit" button does not make make the login call.

The AppState is a singelton class in case you want to know.

Basically it is identical to the form defined in the Flutter documentation: https://flutter.dev/docs/cookbook/forms/validation

import 'package:flutter/material.dart';
import 'package:flutter_poc/app_state.dart';
import 'package:http/http.dart' as http;
import 'dart:convert' as convert;

class AuthPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Center(child: MyCustomForm()),
      ),
    );
  }
}

class MyCustomForm extends StatefulWidget {
  @override
  MyCustomFormState createState() {
    return MyCustomFormState();
  }
}

class MyCustomFormState extends State<MyCustomForm> {
  final _formKey = GlobalKey<FormState>();
  String _username;
  String _password;

  Future<String> getAuthToken(username, password) async {
    final postData = {'username': username, 'password': password};
    final loginUrl = 'https://xxx/login';
    final response = await http.post(loginUrl, body: postData);
    if (response.statusCode == 200) {
      final data = convert.jsonDecode(response.body);
      return data['token'];
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    // Build a Form widget using the _formKey created above.
    return Form(
      key: _formKey,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          TextFormField(
            decoration: InputDecoration(hintText: 'Email'),
            validator: (value) {
              if (value.isEmpty) {
                return 'Enter email';
              }
              return null;
            },
            onSaved: (val) => {_username = val},
          ),
          TextFormField(
            decoration: InputDecoration(hintText: 'Password'),
            validator: (value) {
              if (value.isEmpty) {
                return 'Enter password';
              }
              return null;
            },
            obscureText: true,
            onSaved: (val) => {_password = val},
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 16.0),
            child: RaisedButton(
              onPressed: () async {
                final form = _formKey.currentState;
                if (form.validate()) {
                  form.save();
                  final token = await getAuthToken(_username, _password);
                  if (token == null) {
                    Scaffold.of(context).showSnackBar(
                        SnackBar(content: Text('failed to login')));
                  } else {
                    AppState().setToken(token);
                  }
                }
              },
              child: Text('Submit'),
            ),
          ),
        ],
      ),
    );
  }
}
pjotr_dolphin
  • 1,207
  • 9
  • 34
  • Do you have any errors? Logs would help us determine why you're experiencing that. But you might want to double check your android manifest for permissions like internet permission, or adding cleartextTrafficPermitted – Rick Jan 09 '20 at 18:21
  • 1
    Please don't post question that are basically a link to outside StackOverflow. If you want help from the community you need to post your problem here, that includes the description and the code. https://stackoverflow.com/help/how-to-ask – J. S. Jan 10 '20 at 10:56
  • @Rick no errors at all, just silent... – pjotr_dolphin Jan 10 '20 at 11:29
  • @JoãoSoares thanks for the comment. Updated. – pjotr_dolphin Jan 10 '20 at 11:29
  • 1
    Is it failing on `Debug` and `Release` builds? Can you check if you have the Android internet permission set on your `AndroidManifest.xml` files? – J. S. Jan 10 '20 at 13:24
  • @JoãoSoares the android.permission.INTERNET is set in the debug profile. Right now I'm just running it from VS code DEBUG. – pjotr_dolphin Jan 16 '20 at 08:17
  • I'm sorry, but without an error log, I don't see how can we help you. There doesn't seem to be anything wrong with your code. – J. S. Jan 16 '20 at 08:21
  • In the end it was not a real problem, it was a problem with the emulator that did not have correct dns. And I did not see the error when playing around. More info here: https://github.com/flutter/flutter/issues/48513#issuecomment-575054548 – pjotr_dolphin Jan 16 '20 at 14:53

1 Answers1

0

I didn't notice the problem directly, as I never got any direct response, so always rebooted. But now when I ran in verbose mode I took a cup of coffee, and noticed that there were a DNS resolution error. Basically the Android emulator that I used did not pick up the DNS from the host machine, so it could not resolve the api address. Solved my problem by setting dns manually, like here: Internet stopped working on Android Emulator (Mac OS)

pjotr_dolphin
  • 1,207
  • 9
  • 34