DEV Community

Paolo Mazzon
Paolo Mazzon

Posted on

How to Manage ForeignKey

Hello, I have a page InsertRepair, I'm using RactiveForm and its widgets, for the choise of customers I used reactiveDropodownSearch that how items use a map of repairs loaded by state.customer.name. How to pass relative id at state.customer.name. (foreign key in table repairs on db) To formControl in main Class. I hope that You understand me.

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

import 'package:au79dt1_0/blocs/customer/customer_bloc.dart';
import 'package:au79dt1_0/widgets/custom_dropdw_search.dart';
import 'package:reactive_forms/reactive_forms.dart';

class InsertRepairFirstPage extends StatelessWidget {
  final String? controllerUserId;
  final String? controllerCustomerId;

  final FormGroup formGroup;
  const InsertRepairFirstPage(
      {Key? key,
      this.controllerUserId,
      this.controllerCustomerId,
      required this.formGroup})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        DropDownCustomer(
          nameController: controllerCustomerId,
          formGroup: formGroup,
        )
      ],
    );
  }
}

class DropDownCustomer extends StatelessWidget {
  const DropDownCustomer({
    super.key,
    required this.nameController,
    required this.formGroup,
  });

  final String? nameController;
  final FormGroup formGroup;

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<CustomerBloc, CustomerState>(builder: (context, state) {
      if (state is CustomerGetAllLoadedState) {
        return CustomDropDwSearch(
          formControlName: nameController,
          titlePopUp: 'Select Customers',
          labelText: 'Customers',
          items: state.customers
              .map((customer) => customer.name.toString())
              .toList(),
        );
      }
      return Container();
    });
  }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)