websafe_platform 1.0.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 81

websafe_platform #

Dart Publisher

A Flutter compatible library to detect if the app is Android, iOS, or Web without requiring a hard reference on dart:io, which actually breaks Flutter Web based applications.

Using the library #

Add the repo to your Flutter pubspec.yaml file.

dependencies:
  websafe_platform: <<version>> 

Then run...

flutter packages get

Example #

import 'package:websafe_platform/websafe_platform.dart';

...
var websafePlatform = WebsafePlatform();

var isAndroid = websafePlatform.isAndroid();
var isIOS = websafePlatform.isIOS();
var isWeb = websafePlatform.isWeb();

Unit Testing #

Because most unit tests for plugins do not run in an Android or iOS environment, this supports adding a global override to be able to simulate different platform return values.

import 'package:websafe_platform/websafe_platform.dart';

void main() {
  group('mock', () {
    tearDownAll(() {
      WebsafePlatform.override(null);
    });

    test('android', () {
      WebsafePlatform.override(_MockWebsafePlatform(android: true));
      var websafePlatform = WebsafePlatform();
      expect(true, websafePlatform.isAndroid());
      expect(false, websafePlatform.isIOS());
      expect(false, websafePlatform.isWeb());
    });

    test('ios', () {
      WebsafePlatform.override(_MockWebsafePlatform(ios: true));
      var websafePlatform = WebsafePlatform();
      expect(false, websafePlatform.isAndroid());
      expect(true, websafePlatform.isIOS());
      expect(false, websafePlatform.isWeb());
    });

    test('web', () {
      WebsafePlatform.override(_MockWebsafePlatform(web: true));
      var websafePlatform = WebsafePlatform();
      expect(false, websafePlatform.isAndroid());
      expect(false, websafePlatform.isIOS());
      expect(true, websafePlatform.isWeb());
    });
  });
}

class _MockWebsafePlatform implements WebsafePlatform {
  _MockWebsafePlatform({
    this.android = false,
    this.ios = false,
    this.web = false,
  });

  final bool android;
  final bool ios;
  final bool web;

  @override
  bool isAndroid() => android;

  @override
  bool isIOS() => ios;

  @override
  bool isWeb() => web;
}

[1.0.2] - April 30th, 2020 #

  • Added override option to assest with testing

[1.0.1] - April 27th, 2020 #

  • Transferred to verified publisher

[1.0.0] - April 27th, 2020 #

  • Initial release

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:websafe_platform/websafe_platform.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Websafe Platform Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Websafe Platform Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    var websafePlatform = WebsafePlatform();
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('isAndroid: ${websafePlatform.isAndroid()}'),
            Text('isIOS: ${websafePlatform.isIOS()}'),
            Text('isWeb: ${websafePlatform.isWeb()}'),
          ],
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  websafe_platform: ^1.0.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:websafe_platform/websafe_platform.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
62
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
81
Learn more about scoring.

We analyzed this package on Jul 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.5.0 <3.0.0
meta ^1.1.8 1.2.1
Dev dependencies
test any
test_coverage any