Skip to content

Get Site content after switching from offline to online, can't resolve url #377

@chitgoks

Description

@chitgoks

Hi, you can use this component in runApp(Details()); I am not sure why I get "null check operator used on a null value"?

if there is already an interent connection, it works ok. this problem is when the app is offline.

and, let's say this issue gets fixed. there is also the second problem #2 where it cannot resolve google.com even if it is online. thoughts?

class Detail extends StatefulWidget {

  Detail();

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

class _DetailState extends State<Detail> {
  Future<String?>? _fetch;
  ConnectivityResult _connectionStatus = ConnectivityResult.none;
  final Connectivity _connectivity = Connectivity();
  String? html;
  late StreamSubscription<ConnectivityResult> _connectivitySubscription;

  @override
  void initState() {
    super.initState();
    _initConnectivity();
    _connectivitySubscription = _connectivity.onConnectivityChanged.listen(_updateConnectionStatus);
    _fetch = _fetchPage();
  }

  @override
  void dispose() {
    _connectivitySubscription.cancel();
    super.dispose();
  }

  Future<String?> _fetchPage() async {
    Response response = await Web.getUrl('https://google.com');
    html = response.data;
    return html;
  }

  Widget _getScaffoldBody(bool hasData, ConnectionState state) {
    if (hasData)
      return Center(child: CircularProgressIndicator());
    
    switch (state) {
      case ConnectionState.none:
      case ConnectionState.waiting:
        return Center(child: CircularProgressIndicator());
      default:
        return Text(html!);
    }
  }

  Future<void> _initConnectivity() async {
    late ConnectivityResult result;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      result = await _connectivity.checkConnectivity();
    } on PlatformException catch (e) {
      print(e.toString());
      return;
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) {
      return Future.value(null);
    }

    return _updateConnectionStatus(result);
  }

  Future<void> _updateConnectionStatus(ConnectivityResult result) async {
    setState(() {
      _connectionStatus = result;
    });
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<String?>(
      future: _fetch,
      builder: (context, snapshot) {
        return ThemeSwitchingArea(
          child: Scaffold(
            appBar: AppBar(
              title: Text(AppLocalizations.of(context)!.details),
            ),
            body: _getScaffoldBody(snapshot.hasData, snapshot.connectionState),
          ),
        );
      }
    );
  }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions