3

Simple app that makes calls to free Chuck Norris API and displays its text. Works as linux app and in Android Studio emulator but not when built as APK and side-loaded to my android tablet. Any ideas? Full error from NetworkImage attempt at loading:

SocketException: Failed host lookup: 'assets.chucknorris.host' (OS Error: No address associated with hostname, errno = 7)

My code follows. The _getJoke call to a URL fails silently, probably for same reason.

import 'dart:convert';

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

void main() {
  runApp(const ChuckNorrisApp());
}

class ChuckNorrisApp extends StatelessWidget {
  const ChuckNorrisApp({Key? key}) : super(key: key);
  final String title = "Chuck Norris'isms";

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: title,
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.indigo,
        backgroundColor: Colors.blueGrey,
      ),
      home: _HomePage(title: title),
    );
  }
}

class _HomePage extends StatefulWidget {
  const _HomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<_HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<_HomePage> {
  String _joke = '';

  Future<void> _getJoke() async {
    Uri url =
        Uri(scheme: 'https', host: 'api.chucknorris.io', path: 'jokes/random');
    var result = json.decode(await http.read(url));
    setState(() => _joke = result['value']);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: Row(
        children: [
          Text(widget.title),
          Image(
            image: const NetworkImage(
                'https://assets.chucknorris.host/img/avatar/chuck-norris.png'),
            errorBuilder: (context, error, stackTrace) =>
                SelectableText(error.toString()),
          ),
        ],
      )),
      body: Container(
        decoration: const BoxDecoration(
          color: Colors.blueGrey,
        ),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.all(20.0),
                child: SelectableText(
                  _joke,
                  style: Theme.of(context).textTheme.headline4,
                ),
              ),
            ],
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _getJoke,
        tooltip: 'Next',
        child: const Icon(
          Icons.announcement_rounded,
        ),
      ),
    );
  }
}

2 Answers 2

3

I found this guide and added the following to the AndroidManifest.xml and it now works:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Sign up to request clarification or add additional context in comments.

Comments

1

If you're using a physical device to emulate, don't forget to check if you have Internet turned on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.