avioli / uni_links

Flutter plugin for accepting incoming links.
BSD 2-Clause "Simplified" License
563 stars 303 forks source link

Not every value is working on my code, what I'm doing wrong? #32

Closed mateuslacorte closed 5 years ago

mateuslacorte commented 5 years ago

I'm using https scheme and am testing on my Android Emulator, which is running Android 6: Marshmallow.

They key thing is, I can get the _latestLink to change if it is everything else besides res['message']['access_token'], even res['status'].toString work, which is here in this example...

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:uni_links/uni_links.dart';
import 'package:http/http.dart' as http;
import 'package:url_launcher/url_launcher.dart';
import 'dart:convert';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
  String _latestLink = "";

  StreamSubscription _sub;

  final _scaffoldKey = new GlobalKey<ScaffoldState>();

  @override
  initState() {
    super.initState();
    initPlatformState();
  }

  @override
  dispose() {
    if (_sub != null) _sub.cancel();
    super.dispose();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  initPlatformState() async {
      await initPlatformStateForStringUniLinks();
  }

  initPlatformStateForStringUniLinks() async {

    _sub = getLinksStream().listen((String link) async {
      if (!mounted) return;
      if(RegExp(r'(?<=authorization_token=)(.*)(?=$)').hasMatch(link)) {
        var res = await getToken(link);
        print(res);
        if(res['status']){
          setState(() {
            _latestLink = res['status'].toString();
            try {
            } on FormatException {}
          });
        }
      }
    }, onError: (err) {
      if (!mounted) return;
    });

    if (!mounted) return;
  }

  @override
  Widget build(BuildContext context) {

    return new MaterialApp(
      home: new Scaffold(
        key: _scaffoldKey,
        body: new ListView(
          shrinkWrap: true,
          padding: const EdgeInsets.all(8.0),
          children: <Widget>[
            new ListTile(
              title: const Text('Link'),
              subtitle: new Text('$_latestLink'),
            ),
            homeContent()
          ],
        ),
      ),
    );
  }
}
homeContent(){
  return Center(
      child: RaisedButton(
        onPressed: () async {
          const url = 'https://example.com/anotherPath?client_id=<my client id string, not posting here>';
          if (await canLaunch(url)) {
            await launch(url);
          } else {
            throw 'Could not launch $url';
          }
        },
        child: Text('Signin'),
      )
  );
}

getToken(link) async {
  var req = await http.get("https://example.com/path?client_id=<my client id string, not posting here>&secret=<my secret string, not posting here>&authorization_token=" +  RegExp(r'(?<=authorization_token=)(.*)(?=$)').stringMatch(link));
  var res = json.decode(req.body);
  return res;
}

This code is actually just the example stripped of things.

Any ideia what I might be doing wrong?

mateuslacorte commented 5 years ago

Already got it working.