flutterx_http 1.1.4-dev copy "flutterx_http: ^1.1.4-dev" to clipboard
flutterx_http: ^1.1.4-dev copied to clipboard

outdated

A useful wrapper library of dart.dev http library for HTTP requests.

example/lib/main.dart

import 'package:example/api.dart';
import 'package:example/dto.dart';
import 'package:flutter/material.dart';
import 'package:flutterx_utils/flutterx_utils.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) => MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutterx HTTP Demo',
      theme: ThemeData(primarySwatch: Colors.lightGreen),
      home: const HTTPExample());
}

class HTTPExample extends StatefulWidget {
  const HTTPExample({Key? key}) : super(key: key);

  @override
  State<HTTPExample> createState() => _HTTPExampleState();
}

class _HTTPExampleState extends State<HTTPExample> {
  static const String _apiKey = 'f8af9d32473e8c46afeeb160d6306a28';
  final OpenWeather _api = OpenWeather(_apiKey);
  final TextEditingController _city = TextEditingController(text: 'Rome, IT');
  Units _units = Units.metric;
  Units _currentUnits = Units.metric;
  Result<WeatherInfo>? _data;
  bool _calling = false;

  @override
  Widget build(BuildContext context) => Scaffold(
      appBar: AppBar(title: const Text('HTTP example')),
      body: Center(
          child: Padding(
              padding: const EdgeInsets.all(32),
              child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
                Row(children: [
                  SizedBox(
                      width: MediaQuery.of(context).size.width * .6,
                      height: 38,
                      child: TextField(
                          controller: _city,
                          decoration:
                              const InputDecoration(isDense: true, icon: Icon(Icons.location_pin), hintText: 'City'))),
                  DropdownButton<Units>(
                      value: _units,
                      onChanged: (value) => setState(() => _units = value ?? _units),
                      items: Units.values
                          .map((unit) => DropdownMenuItem<Units>(value: unit, child: Text(unit.unit)))
                          .toList(growable: false)),
                ]),
                const Spacer(),
                ..._weatherPreview,
                const Spacer(flex: 2),
              ]))),
      floatingActionButton: FloatingActionButton(
          onPressed: _getWeatherPreview, tooltip: 'Get weather', child: const Icon(Icons.wb_sunny)));

  Iterable<Widget> get _weatherPreview {
    if (_calling) return const [Text('Performing call...')];
    final result = _data;
    if (result == null)
      return const [Text('Welcome to flutterx_http API sample.\nPress the FAB to get weather preview for your city')];
    return result.handle<Iterable<Widget>>(
        onSuccess: (value) sync* {
          for (final weather in value.weather)
            yield Row(mainAxisSize: MainAxisSize.min, children: [
              Image.network(_api.imageUrl(weather), width: 42, height: 42),
              Text(weather.description.capitalized, style: Theme.of(context).textTheme.subtitle1),
            ]);
          yield Text(
              'There are ${value.main.temp.round()}${_currentUnits.unit} in ${value.name} (${value.sys.country})\nmin: ${value.main.tempMin.round()}${_currentUnits.unit}\nmax: ${value.main.tempMax.round()}${_currentUnits.unit}\nhumidity: ${value.main.humidity}%',
              style: Theme.of(context).textTheme.bodyText2);
        },
        onError: (error) => [Text('Failed to get weather preview:\n${error.description}')]);
  }

  Future<void> _getWeatherPreview() async {
    FocusScope.of(context).unfocus();
    setState(() => _calling = true);
    _data = await Result.wrapAsync(() => _api.getWeather(city: _city.value.text, units: _units));
    setState(() {
      _currentUnits = _units;
      _calling = false;
    });
  }
}
1
likes
0
points
33
downloads

Publisher

unverified uploader

Weekly Downloads

A useful wrapper library of dart.dev http library for HTTP requests.

Repository (GitLab)
View/report issues

License

unknown (license)

Dependencies

flutter, flutterx_utils, http, http_parser

More

Packages that depend on flutterx_http