1

First of all my Firebase structure looks like this:

Firebase

I want to get for example, CCV value and show it in my app GUI which looks like this:

GUI

All the card entries are structured in my code in a widget called cardEntries() and I think there I should build my StreamBuilder method, but I don't know a well structured method that can work.

3
  • The FlutterFire repo contain a full example, which I often copy bits from. See github.com/flutter/plugins/blob/master/packages/… Commented Nov 9, 2018 at 15:04
  • @FrankvanPuffelen not quite useful, because its very complex and I dont fully understand the code there. Need something more precise. Commented Nov 9, 2018 at 15:07
  • Without seeing what you tried, it'll be hard to help better than any sample/docs. Commented Nov 9, 2018 at 16:14

1 Answer 1

1

You can use something similar to BloC pattern to implement Firebase Realtime Database on a StreamBuilder.

Create a model for your Card Info first. You can also include the json parser on the same class.

class CardInfo{
  String? cvv;
  String? cardHolder;
  String? number;
  String? expiry;
  CardInfo({this.cvv, this.cardHolder,
      this.number, this.expiry});

  factory CardInfo.fromJson({required Map<dynamic,dynamic> json}){
    return CardInfo(
      cvv: json['CVV'],
      cardHolder: json['Card Holder'],
      number: json['Credit Card Number'],
      expiry: json['Expiry Date'],
    );
  }
}

Add your Streams on a Bloc class

class Bloc {
  final _repository = Repository();
  final _cardInfoFetcher = StreamController<CardInfo>.broadcast();
  
  /// The Stream that will be used on StreamBuilder
  Stream<CardInfo> get userCardInfo => _cardInfoFetcher.stream;

  fetchUserThreads(String? userId) async {
    var cardInfo = CardInfo();

    /// Query user card details
    var userCardDetails = FirebaseDatabase.instance.reference().child('Users').child(userId)
      .child('Card Info');

    /// Fetch user card details once
    var snapshot = await userCardDetails.once();

    Map<dynamic, dynamic> userCardSnapshot = snapshot.value;
    _cardInfoFetcher.sink.add(CardInfo.fromJson(json: userCardSnapshot));
  }

  /// Dispose Stream when not in use
  dispose() {
    _cardInfoFetcher.close();
  }
}
final bloc = Bloc();

Every time bloc.fetchUserThreads(String) is called, StreamBuilder should do a widget rebuild.

StreamBuilder<CardInfo>(
  stream: bloc.userCardInfo,
  builder: (BuildContext context,
      AsyncSnapshot<CardInfo> snapshot) {
    if (!snapshot.hasData)
      debugPrint('Snapshot empty');
    else {
      debugPrint('Card Holder: ${snapshot.data!.cardHolder}');
    }
    /// You can now populate child widget with CardInfo data from the snapshot
    return Container();
  }
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.