title: OAuth2 authentication in Flutter
body: <p>I can not authenticate with Flutter and OAuth2. Basically I created a Flutter base project and added the <a href="https://pub.dartlang.org/packages/oauth2" rel="nofollow noreferrer">example</a> of the OAuth2 wiki.</p>
  
  <p>This is the class from Flutter basic project:</p>
  
  <pre><code>class _MyHomePageState extends State&lt;MyHomePage&gt; {
    int _counter = 0;
  
    void _login() async {
      final authorizationEndpoint =
      Uri.parse("http://blablablabla/o/token");
  
      final username = "username";
      final password = "password";
  
      // Something like this
      final identifier = "mavEZjJgs9d4JwvvXsANZgN5Dz5GFxzfj616752A";
      final secret = "hcAFRwvtGqzhKrMPH2Vqm1vncuZt2YTVfTs6LcdNcnKPdEH3J0T1njIwurryofrvDMnzOvhQDVbaC9Gt5DctciTv3n89s7JSGjpHtzkbEfLpkOT5y6YHN3p6grQlYGd59";
  
      // Make a request to the authorization endpoint that will produce the fully
      // authenticated Client.
      var client = await oauth2.resourceOwnerPasswordGrant(
          authorizationEndpoint, username, password,
          identifier: identifier, secret: secret);
  
      // Once you have the client, you can use it just like any other HTTP client.
      var result = await client.read("http://blablabla/api/users/me/");
  
      // Once we're done with the client, save the credentials file. This will allow
      // us to re-use the credentials and avoid storing the username and password
      // directly.
      new File("~/.myapp/credentials.json")
          .writeAsString(client.credentials.toJson());
    }
  
    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        appBar: new AppBar(
          title: new Text(widget.title),
        ),
        body: new Center(
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: &lt;Widget&gt;[
              new Text(
                'You have pushed the button this many times:',
              ),
              new Text(
                '$_counter',
                style: Theme.of(context).textTheme.display1,
              ),
            ],
          ),
        ),
        floatingActionButton: new FloatingActionButton(
  //        onPressed: _incrementCounter,
          onPressed: _login,
          tooltip: 'Increment',
          child: new Icon(Icons.add),
        ), // This trailing comma makes auto-formatting nicer for build methods.
      );
    }
  }
  </code></pre>
  
  <p>I changed the <code>_incrementCounter</code> method of the tutorial to <code>_login</code>. When I press the button (which I should now log me in) I get the following:</p>
  
  <pre><code>E/flutter ( 7662): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
  E/flutter ( 7662): NoSuchMethodError: No top-level method 'base64Encode' declared.
  E/flutter ( 7662): Receiver: top-level
  E/flutter ( 7662): Tried calling: base64Encode(Uint8Array)
  E/flutter ( 7662): #0      NoSuchMethodError._throwNew (dart:core-patch/dart:core/errors_patch.dart:192)
  E/flutter ( 7662): #1      basicAuthHeader (package:oauth2/src/utils.dart:14)
  E/flutter ( 7662): #2      resourceOwnerPasswordGrant (package:oauth2/src/resource_owner_password_grant.dart:69)
  E/flutter ( 7662): &lt;asynchronous suspension&gt;
  E/flutter ( 7662): #3      _MyHomePageState._login (file:///home/thisismyuser/AndroidStudioProjects/login/lib/main.dart:79)
  E/flutter ( 7662): &lt;asynchronous suspension&gt;
  E/flutter ( 7662): #4      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:478)
  E/flutter ( 7662): #5      _InkResponseState.build.&lt;anonymous closure&gt; (package:flutter/src/material/ink_well.dart:530)
  E/flutter ( 7662): #6      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102)
  E/flutter ( 7662): #7      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:161)
  E/flutter ( 7662): #8      TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:123)
  E/flutter ( 7662): #9      GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156)
  E/flutter ( 7662): #10     BindingBase&amp;GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:147)
  E/flutter ( 7662): #11     BindingBase&amp;GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121)
  E/flutter ( 7662): #12     BindingBase&amp;GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101)
  E/flutter ( 7662): #13     BindingBase&amp;GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64)
  E/flutter ( 7662): #14     BindingBase&amp;GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48)
  E/flutter ( 7662): #15     _invoke1 (file:///b/build/slave/Linux_Engine/build/src/flutter/lib/ui/hooks.dart:134)
  E/flutter ( 7662): #16     _dispatchPointerDataPacket (file:///b/build/slave/Linux_Engine/build/src/flutter/lib/ui/hooks.dart:91)
  </code></pre>
  
  <p>Thanks in advance!</p>
  
  <p>(With Postman I get this beautiful authentication)</p>
  
  <p><a href="https://i.stack.imgur.com/G0MqG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/G0MqG.png" alt="enter image description here"></a></p>