Zfinix / carrier_info

📱 Carrier Info gets networkType, networkGeneration, mobileCountryCode, mobileCountryCode, e.t.c from both android and iOS devices.
MIT License
34 stars 48 forks source link

Example Code not work #16

Closed ellynhan closed 2 years ago

ellynhan commented 2 years ago

I tried to use this package, but the example code didn't work for me.

What parts that I changed are,

  1. CarrierData carrierInfo initializing

    • I tried to use pure example code, but error occured.
    • error: Non-nullable instance field 'carrierInfo' must be initialized.
    • So I added 'late' modifier, but It also didn't work.
    • My last trial, I initialized it 'CarrierData carrierInfo = CarrierData(); '
  2. remove ? from HomeItem value

    • I got warning : Warning: Operand of null-aware operation '?.' has type 'CarrierData' which excludes null.
    • So I removed '?' like, value: '${carrierInfo.carrierName}',

The code is below,

import 'package:flutter/cupertino.dart';
import 'dart:async';
import 'package:carrier_info/carrier_info.dart';
import 'package:flutter/material.dart';

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

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

class _MyAppState extends State<MyApp> {
  CarrierData carrierInfo = CarrierData();

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

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    try {
      carrierInfo = (await CarrierInfo.all)!;
      setState(() {});
    } catch (e) {
      print(e.toString());
    }
  }

  @override
  Widget build(BuildContext context) {
    return CupertinoApp(
      debugShowCheckedModeBanner: false,
      home: CupertinoPageScaffold(
        navigationBar: CupertinoNavigationBar(
          middle: const Text('Carrier Info example app'),
          border: Border.symmetric(
            horizontal: BorderSide(
              width: 0.5,
              color: CupertinoColors.systemGrey2.withOpacity(0.4),
            ),
          ),
        ),
        backgroundColor: CupertinoColors.lightBackgroundGray,
        child: ListView(
          children: [
            const SizedBox(
              height: 20,
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const Padding(
                  padding: EdgeInsets.all(15),
                  child: Text(
                    'CARRIER INFORMATION',
                    style: TextStyle(
                      fontSize: 11,
                      color: CupertinoColors.systemGrey,
                    ),
                  ),
                ),
                HomeItem(
                  title: 'Name',
                  value: '${carrierInfo.carrierName}',
                  isFirst: true,
                ),
                HomeItem(
                  title: 'Country Code',
                  value: '${carrierInfo.isoCountryCode}',
                ),
                HomeItem(
                  title: 'Mobile Country Code',
                  value: '${carrierInfo.mobileCountryCode}',
                ),
                HomeItem(
                  title: 'Mobile Network Operator',
                  value: '${carrierInfo.mobileNetworkOperator}',
                ),
                HomeItem(
                  title: 'Mobile Network Code',
                  value: '${carrierInfo.mobileNetworkCode}',
                ),
                HomeItem(
                  title: 'Allows VOIP',
                  value: '${carrierInfo.allowsVOIP}',
                ),
                HomeItem(
                  title: 'Radio Type',
                  value: '${carrierInfo.radioType}',
                ),
                HomeItem(
                  title: 'Network Generation',
                  value: '${carrierInfo.networkGeneration}',
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}

class HomeItem extends StatelessWidget {
  final bool? isFirst;
  final String? title;
  final String? value;
  const HomeItem({
    Key? key,
    @required this.title,
    this.value,
    this.isFirst = false,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        children: [
          if (!isFirst!)
            Container(height: 0.5, color: Colors.grey.withOpacity(0.3)),
          Padding(
            padding: const EdgeInsets.all(15),
            child: Row(
              children: [
                Text(title ?? ''),
                Spacer(),
                Text(value ?? ''),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

and when I build this code, I got

Name - null Country Code - null Mobile Country Code - null ... Allow VOIP - false Radi Type - null Network Generation - null

and I think, it's just contents of CarrierData(). Is there any permission to check? or something else? what should I do for using this package? 😢

nicolaspernoud commented 2 years ago

Please try the new version (after my pull request) and relaunch the app after accepting the permissions.

Zfinix commented 2 years ago

Thanks @nicolaspernoud

Zfinix commented 2 years ago

Successfully uploaded https://pub.dev/packages/carrier_info "2.0.4".

On Thu, Jan 6, 2022 at 3:36 PM Nicolas PERNOUD @.***> wrote:

Please try the new version (after my pull request) and relaunch the app after accepting the permissions.

— Reply to this email directly, view it on GitHub https://github.com/Zfinix/carrier_info/issues/16#issuecomment-1006639613, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGE3JSNIM36M53UL4GV342DUUWSHDANCNFSM5LMMCSJA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

ellynhan commented 2 years ago

Successfully uploaded https://pub.dev/packages/carrier_info "2.0.4". On Thu, Jan 6, 2022 at 3:36 PM Nicolas PERNOUD @.> wrote: Please try the new version (after my pull request) and relaunch the app after accepting the permissions. — Reply to this email directly, view it on GitHub <#16 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGE3JSNIM36M53UL4GV342DUUWSHDANCNFSM5LMMCSJA . You are receiving this because you are subscribed to this thread.Message ID: @.>

oh, now It works!! Thank you :)

And I curious,
does it only work on mobile data environment? Is it correct result of all-null on wifi environment?

nicolaspernoud commented 2 years ago

Glad that it works ! If your connected to wifi AND mobile network, you will get data for mobile network. But if your are connected only to wifi, it will not output carrier data.

Zfinix commented 2 years ago

Thanks for the tip!

On Thu, 6 Jan 2022 at 3:59 PM, Nicolas PERNOUD @.***> wrote:

Glad that it works ! If your connected to wifi AND mobile network, you will get data for mobile network. But if your are connected only to wifi, it will not output carrier data.

— Reply to this email directly, view it on GitHub https://github.com/Zfinix/carrier_info/issues/16#issuecomment-1006657260, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGE3JSOBPALWQTYGBL452JDUUWU5FANCNFSM5LMMCSJA . You are receiving this because you commented.Message ID: @.***>

-- Chiziaruhoma O.

ellynhan commented 2 years ago

I see, thank you all! 👍👍👍