Flutter Firestore数据未显示在应用程序中

时间:2019-09-24 08:00:00

标签: flutter dart visual-studio-code

我是扑扑和信标技术的新手。实际上,我想在Firestore(“设备”)以及iBeacon uuid中调用该项目并将其放在ListView中,但该应用程序没有出现错误,但是没有显示任何数据。我不知道问题出在哪里,希望你们能帮助我。预先感谢

这是main.dart

void main() {
  runApp(new FlutterBlueApp());
}

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

  final String title;

  @override
  _FlutterBlueAppState createState() => new_FlutterBlueAppState();
}

class _FlutterBlueAppState extends State<FlutterBlueApp> {
  FlutterBlueBeacon flutterBlueBeacon =FlutterBlueBeacon.instance;
  FlutterBlue _flutterBlue = FlutterBlue.instance;

  /// Scanning
  StreamSubscription _scanSubscription;
  Map<int, Beacon> beacons = new Map();
  bool isScanning = false;

  /// State
  StreamSubscription _stateSubscription;
  BluetoothState state = BluetoothState.unknown;

  @override
  void initState() {
   super.initState();
   // Immediately get the state of FlutterBlue
    _flutterBlue.state.then((s) {
      setState(() {
        state = s;
      });
    });
    // Subscribe to state changes
    _stateSubscription = _flutterBlue.onStateChanged().listen((s) {
      setState(() {
        state = s;
      });
    });
  }

  @override
  void dispose() {
    _stateSubscription?.cancel();
    _stateSubscription = null;
    _scanSubscription?.cancel();
    _scanSubscription = null;
    super.dispose();
  }

  _clearAllBeacons() {
    setState(() {
      beacons = Map<int, Beacon>();
    });
  }

  _startScan() {
    print("Scanning now");
    _scanSubscription = flutterBlueBeacon
        .scan(timeout: const Duration(hours: 24))
        .listen((beacon) {
      print('localName: ${beacon.scanResult.advertisementData.localName}');
      print('manufacturerData: ${beacon.scanResult.advertisementData.manufacturerData}');
      print('serviceData: ${beacon.scanResult.advertisementData.serviceData}');
      setState(() {
        beacons[beacon.hash] = beacon;
      });
    }, onDone: _stopScan);

    setState(() {
      isScanning = true;
    });
  }

  _stopScan() {
    print("Scan stopped");
    _scanSubscription?.cancel();
    _scanSubscription = null;
    setState(() {
      isScanning = false;
    });
  }

void _buildScanningButton() {
if (state != BluetoothState.on) {
  return null;
}
if (isScanning) {
  return _stopScan();
} else {
  return _startScan();
}
  }

  _buildScanResultTiles() {
return beacons.values.map<Widget>((b) {
  if (b is IBeacon) {
    new Padding(
      padding: EdgeInsets.all(8.0),
      child: StreamBuilder(
        stream: Firestore.instance.collection("Devices").snapshots(),
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if (!snapshot.hasData)
          return new Container(
            child: Center(
              child: _buildProgressBarTile(),
            ),
          );
          return IBeaconCard( document: snapshot.data.documents, iBeacon: b,);
        },
      ),
    );
  }
  if (b is EddystoneUID) {
    return EddystoneUIDCard(eddystoneUID: b);
  }
  if (b is EddystoneEID) {
    return EddystoneEIDCard(eddystoneEID: b);
  }
  return Card();
}).toList();
  }

  _buildAlertTile() {
return new Container(
  color: Colors.redAccent,
  child: new ListTile(

    title: new Text(
      'Bluetooth adapter is 
${state.toString().substring(15)}',
      style: Theme.of(context).primaryTextTheme.subhead,
    ),
    trailing: new Icon(
      Icons.error,
      color: Theme.of(context).primaryTextTheme.subhead.color,
    ),
  ),
);
  }

  _buildProgressBarTile() {
return new LinearProgressIndicator();
  }


  @override
  Widget build(BuildContext buildcontext) {
var tiles = new List<Widget>();
if (state != BluetoothState.on) {
  tiles.add(_buildAlertTile());
}

tiles.addAll(_buildScanResultTiles());

return new MaterialApp(
  home: StreamBuilder<Object>(
    stream: null,
    builder: (context, snapshot) {
      return new Scaffold(
        appBar: new AppBar(
          title: const Text('Panic Bluetooth Device'),
          actions: <Widget>[],
        ),
        body: new Stack(
          children: <Widget>[
            (isScanning) ? _buildProgressBarTile() : new Container(),
            new ListView(
              children: tiles,
            )
          ],
        ),
        floatingActionButton: SpeedDial(
          child: Icon(Icons.menu),
          children: [
            SpeedDialChild(
                child: Icon(Icons.search),
                label: "Seacrh Bluetooth Devices",
                onTap: () => _buildScanningButton()),
                SpeedDialChild(
                child: Icon(Icons.add),
                label: "Add Bluetooth Devices",
                onTap: () => Navigator.push(context,
                    new MaterialPageRoute(builder: (buildcontext) => AddDevices()))),
          ],
        ),
      );
    }
  ),
);
  }
}

这是widget.dart

class IBeaconCard extends StatelessWidget {
  IBeaconCard({this.iBeacon, this.document});

  final IBeacon iBeacon;
  final List<DocumentSnapshot> document;

  @override
  Widget build(BuildContext context) {
      return new ListView.builder(
        itemBuilder: (BuildContext context, int i) {
      String owner = document[i].data['Owner'].toString();
      String devicesId = iBeacon.uuid.toString();
      String latitude = document[i].data['Latitude'].toString();
      String longitude = document[i].data['Longitude'].toString();

      return Card(
        child: Column(
            children: <Widget>[
            Text(owner),
            Text(devicesId),
            Text(latitude),
            Text(longitude)
            ],
            ),
          );

    },
  );
 }
 }

0 个答案:

没有答案