【Flutter】Firebase検索とソート

  void fire() async {
    var msg = _controller.text;
    FirebaseFirestore firestore = FirebaseFirestore.instance;
    final snapshot = await firestore.collection('mydata')
      .where('name', isEqualTo: msg).get();
    snapshot.docChanges.forEach((element) {
      final name = element.doc.get('name');
      final mail = element.doc.get('mail');
      final age = element.doc.get('age');
      msg += "\n${name} (${age}) <${mail}>";
    });
    _controller.text = msg;
  }

final snapshot = await firestore.collection(‘mydata’)
.orderBy(‘name’, descending: false)
.startAt([msg])
.endAt([msg + ‘\utf8ff’])
.get();

descendingはflaseだと昇順、trueだと降順にデータが並べられる

### データの追加

  void addDoc() async {
    var msg = _controller.text;
    final input = msg.split(',');
    final data = {
      'name': input[0],
      'mail': input[1],
      'age': input[2]
    };
    FirebaseFirestore firestore = FirebaseFirestore.instance;
    final snapshot = await firestore.collection('mydata')
      .add(data);
    fire();
  }

  void fire() async {
    var msg = '';
    FirebaseFirestore firestore = FirebaseFirestore.instance;
    final snapshot = await firestore.collection('mydata')
      .orderBy('name', descending: false).get();
    snapshot.docChanges.forEach((element) {
      final name = element.doc.get('name');
      final mail = element.doc.get('mail');
      final age = element.doc.get('age');
      msg += "\n${name} (${age}) <${mail}>";
    });
    _controller.text = msg;
  }