Flutter generalized parameter? - flutter

I have a flutter app that uses a database what is generated by Moor.
Normally I would need 3 pages but since all 3 pages would look the same, the code would be the same. Only difference is the DAO they use.
So I have FirstDao, SecondDao, ThirdDao. All of them have the list() method and all of them return different type of objects (FirstType, SecondType, ThirdType) At the moment this is how I made it work but I am not sure is this a good way:
class RollingListChoice<T> extends StatefulWidget {
T dao;
RollingListChoice(this.dao);
#override
_RollingListChoiceState createState() =>
_RollingListChoiceState(dao);
}
class _RollingListChoiceState<T> extends State<RollingListChoice> {
T dao;
_RollingListChoiceState(this.dao);
#override
Widget build(BuildContext context) {
return Text(getDao().list());
}
getDao() {
if (dao.runtimeType == FirstDao) {
FirstDao mydao = dao as FirstDao;
return mydao;
} else if (dao.runtimeType == SecondDao) {
SecondDaomydao = dao as SecondDao;
return mydao;
} else if (dao.runtimeType == ThirdDao) {
ThirdDao mydao = dao as ThirdDao;
return mydao;
}
}
}
Any way to improve the code? Is there any best practice for this type of problem?

you can have an interface for DAO classes (for example IDAO) and replace RollingListChoice<T> with RollingListChoice<T extends IDAO> to restrict type T to specific classes. by that, you don't need to cast dao in getDao() method and you will be sure that dao has list() method.
abstract class IDAO {
String list();
}
class RollingListChoice<T extends IDAO> extends StatefulWidget {
T dao;
RollingListChoice(this.dao);
#override
_RollingListChoiceState createState() =>
_RollingListChoiceState(dao);
}
class _RollingListChoiceState<T extends IDAO> extends State<RollingListChoice> {
T dao;
_RollingListChoiceState(this.dao);
#override
Widget build(BuildContext context) => Text(dao.list());
}
if you do mentioned changes you don't need to getDao() method, but you can change getDao() method like this as well:
getDao() {
if (dao is FirstDao) {
FirstDao mydao = dao as FirstDao;
return mydao;
} else if (dao is SecondDao) {
SecondDaomydao = dao as SecondDao;
return mydao;
} else if (dao is ThirdDao) {
ThirdDao mydao = dao as ThirdDao;
return mydao;
}
}

Related

How to make inheritance hierarchy for Type Converter of Dart Floor?

As below, I define ListConverter<T> to make inheritance hierarchy.
abstract class ListConverter<T> extends TypeConverter<List<T>, String> {
static const delimiter = "!DELIMITER!";
T fromDB(String databaseValue);
String toDB(T value);
#override
List<T> decode(String databaseValue) {
return databaseValue
.split(delimiter)
.where((element) => element.isNotEmpty)
.map((e) => fromDB(e))
.toList();
}
#override
String encode(List<T> value) {
return value.map((e) => toDB(e)).join(delimiter);
}
}
Using ListConverter<T>, I implement IntListConverter.
class IntListConverter extends ListConverter<int> {
#override
int fromDB(String databaseValue) {
return int.parse(databaseValue);
}
#override
String toDB(int value) {
return value.toString();
}
}
My plan was to use ListConverter<T> also on StringListConverter, etc.
But unfortunately, I got this error message while running floor_generator.
[SEVERE] floor_generator:floor_generator on lib/persistence/db/app_database.dart:
Only classes that inherit from TypeConverter can be used as type converters. Make sure use a class that inherits from TypeConverter.
I know that Hierarchy (B) works well, but I want to build (A) structure for managing other list converters.
(A) TypeConverter -> ListConverter -> IntListConverter
(B) TypeConverter -> IntListConverter
How to make inheritance hierarchy for Type Converter of Dart Floor?
I resolved this problem in a roundabout way.
I use mixin keyword instead of direct inheritance.
class IntListConverter extends TypeConverter<List<int>, String>
with ListConverter<int> {
#override
int fromDB(String databaseValue) {
return int.parse(databaseValue);
}
#override
String toDB(int value) {
return value.toString();
}
}

Best way to shrink service class to smaller pieces

Currently making a large app with Flutter and I m stuck on the architecture of service class. There is a service class for the firestore CRUD operations.This class has many methods and I want split it into small pieces. I use an abstract class to protect methods.I find a way with mixins but don't know it's a good one or not.
https://gist.github.com/pMertDogan/fcd301d768f3980a898cec33a9acaa4f.
//Extend CRUDSERVICE rules aka abstract class => Test
mixin Update{
void updateSomething();
}
mixin Read{
void readSomething();
}
//BASE class for CRUDSERVICE
abstract class Test with Update,Read{
doSomeCreateOP(String x);
}
//
class CrudService extends Test with UpdateService , ReadService{
#override
doSomeCreateOP(String x) {
print('crated ' + x);
}
}
mixin UpdateService{
// #override
void updateSomething() {
print('updated');
}
}
mixin ReadService{
// #override
void readSomething() {
print('read');
}
}
void main() {
CrudService croudService = CrudService();
croudService.doSomeCreateOP(' dartSide');
croudService.updateSomething();
croudService.readSomething();
}
CreateService and UpdateService mixins are just sample.I am thinking like if I need update user information all methods are handed by UserServiceMix mixin if it's Friend then its hanled by FriendServiceMix so I can split them like a domain-based.Each mixin is responsible for specific operations.I can manage then on independent files and summary of them with the help of mixin.
Is it good way to go?
I believe it is a good way to go. It is a quite flexible approach. We use it for API versioning as well.
abstract class Service {
void method1();
void method2();
}
mixin Method1V1 {
void method1() {
print("method1");
}
}
mixin Method2V1 {
void method2() {
print("method2");
}
}
mixin Method2V2 {
void method2() {
print("method2 with changed logic");
}
}
class ServiceV1 extends Service with Method1V1, Method2V1 {
}
class ServiceV2 extends Service with Method1V1, Method2V2 {
}
void main() {
final version = 2;
final Service service = version == 1 ? ServiceV1() : ServiceV2();
service.method2();
}

Dart & flutter : How can I pass an implementation of an abstract class to the constructor of another class? [duplicate]

Lets say that I have an abstract class
abstract class OnClickHandler {
void doA();
void doB();
}
I have a class
class MyClass {
OnClickHandler onClickHandler;
MyClass({
this.onClickHandler
})
void someFunction() {
onClickHandler.doA();
}
}
And I have a class
class Main implements onClickHandler {
// This throws me an error
MyClass _myClass = MyClass(onClickHandler = this); // <- Invalid reference to 'this' expression
#override
void doA() {}
#override
void doB() {}
}
How can I say that use the same implementations that the Main class has? or is there an easier/better way to do this?
Your problem is that this does not yet exists since the object are still being created. The construction of Dart objects is done in two phases which can be difficult to understand.
If you change you program to the following it will work:
abstract class OnClickHandler {
void doA();
void doB();
}
class MyClass {
OnClickHandler onClickHandler;
MyClass({this.onClickHandler});
void someFunction() {
onClickHandler.doA();
}
}
class Main implements OnClickHandler {
MyClass _myClass;
Main() {
_myClass = MyClass(onClickHandler: this);
}
#override
void doA() {}
#override
void doB() {}
}
The reason is that code running inside { } in the constructor are executed after the object itself has been created but before the object has been returned from the constructor.

Polymorphism in Dart/Flutter

I have a number of classes that all extend StatefulWidget. I want each class to provide its own version of a method bool foo(), so that I can iterate over a collection of objects of these classes, calling foo() on each. What is the best/correct way to do this in Dart/Flutter? Mixins? What would the type of that collection be?
The functionality described can be achieved with Interfaces in Dart
class Widget {
}
abstract class MyCustomWidget extends Widget {
String foo( String argName);
}
class Widget1 implements MyCustomWidget {
String foo( String argName) {
return argName;
}
}
class Widget2 implements MyCustomWidget {
String foo( String argName) {
return '$argName$argName';
}
}
void main() {
Widget1 w = new Widget1();
Widget2 w2 = new Widget2();
var widgets = [w, w2];
for (int i = 0; i < widgets.length; i++) {
print(widgets[i].foo('hello ${i + 1}'));
}
}
That said, flutter recommends composition over inheritance.
See: Flutter StatefulWidget - State class inheritance?

GEF + EMF: Why doesn't my editor remove the Figure for a removed object when refreshChildren() is called?

I have implemented a GEF editor for a graph-like EMF model, with a remove command for a certain type of node in the graph. I think I've done all the necessary steps in order to make this set up work (vainolo's blog has been a great help).
However, when I'm deleting a model element, the view doesn't get refreshed, i.e., the figure for the model element isn't removed from the editor view, and I have no idea why. I'd be extremely grateful if somebody could have a look at my sources and point me to any problems (and possibly solutions :)). Many thanks in advance!
Below are what I think are the important classes for this issue. Please do let me know should I add further code/edit the code, etc. (I've left out code that I thought doesn't help, e.g., getters and setters, class variables). Thanks!
DiagramEditPart
public class DiagramEditPart extends AbstractGraphicalEditPart {
public DiagramEditPart(Diagram model) {
this.setModel(model);
adapter = new DiagramAdapter();
}
#Override protected IFigure createFigure() {
Figure figure = new FreeformLayer();
return figure;
}
#Override protected void createEditPolicies() {
installEditPolicy(EditPolicy.LAYOUT_ROLE, new DiagramXYLayoutPolicy());
}
#Override protected List<EObject> getModelChildren() {
List<EObject> allModelObjects = new ArrayList<EObject>();
if (((Diagram) getModel()).getMyNodes() != null)
allModelObjects.addAll(((Diagram) getModel()).getMyNodes());
return allModelObjects;
}
#Override public void activate() {
if(!isActive()) {
((Diagram) getModel()).eAdapters().add(adapter);
}
super.activate();
}
#Override public void deactivate() {
if(isActive()) {
((Diagram) getModel()).eAdapters().remove(adapter);
}
super.deactivate();
}
public class DiagramAdapter implements Adapter {
#Override public void notifyChanged(Notification notification) {
switch (notification.getEventType()) {
case Notification.REMOVE: refreshChildren();
break;
default:
break;
}
}
#Override public Notifier getTarget() {
return (Diagram) getModel();
}
#Override public void setTarget(Notifier newTarget) {
// Do nothing.
}
#Override public boolean isAdapterForType(Object type) {
return type.equals(Diagram.class);
}
}
}
MyNodeEditPart
public class MyNodeEditPart extends AbstractGraphicalEditPart {
public MyNodeEditPart(MyNode model) {
this.setModel(model);
adapter = new MyNodeAdapter();
}
#Override protected IFigure createFigure() {
return new MyNodeFigure();
}
#Override protected void createEditPolicies() {
installEditPolicy(EditPolicy.COMPONENT_ROLE, new MyNodeComponentEditPolicy());
}
#Override protected void refreshVisuals() {
MyNodeFigure figure = (MyNodeFigure) getFigure();
DiagramEditPart parent = (DiagramEditPart) getParent();
Dimension labelSize = figure.getLabel().getPreferredSize();
Rectangle layout = new Rectangle((getParent().getChildren().indexOf(this) * 50),
(getParent().getChildren().indexOf(this) * 50), (labelSize.width + 20),
(labelSize.height + 20));
parent.setLayoutConstraint(this, figure, layout);
}
public List<Edge> getModelSourceConnections() {
if ((MyNode) getModel() != null && ((MyNode) getModel()).getDiagram() != null) {
ArrayList<Edge> sourceConnections = new ArrayList<Edge>();
for (Edge edge : ((MyNode) getModel()).getDiagram().getOutEdges(((MyNode) getModel()).getId())) {
sourceConnections.add(edge);
}
return sourceConnections;
}
return null;
}
// + the same method for targetconnections
#Override public void activate() {
if (!isActive()) {
((MyNode) getModel()).eAdapters().add(adapter);
}
super.activate();
}
#Override public void deactivate() {
if (isActive()) {
((MyNode) getModel()).eAdapters().remove(adapter);
}
super.deactivate();
}
public class MyNodeAdapter implements Adapter {
#Override
public void notifyChanged(Notification notification) {
refreshVisuals();
}
#Override
public Notifier getTarget() {
return (MyNode) getModel();
}
#Override
public void setTarget(Notifier newTarget) {
// Do nothing
}
#Override
public boolean isAdapterForType(Object type) {
return type.equals(MyNode.class);
}
}
}
MyNodeComponentEditPolicy
public class MyNodeComponentEditPolicy extends ComponentEditPolicy {
#Override
protected Command createDeleteCommand(GroupRequest deleteRequest) {
DeleteMyNodeCommand nodeDeleteCommand = new DeleteMyNodeCommand((MyNode) getHost().getModel());
return nodeDeleteCommand;
}
}
DeleteMyNodeCommand
public class DeleteMyNodeCommand extends Command {
public DeleteMyNodeCommand(MyNode model) {
this.node = model;
this.graph = node.getDiagram();
}
#Override public void execute() {
getMyNode().setDiagram(null);
System.out.println("Is the model still present in the graph? " + getGraph().getMyNodes().contains(getMyNode()));
// Returns false, i.e., graph doesn't contain model object at this point!
}
#Override public void undo() {
getMyNode().setDiagram(getGraph());
}
}
EDIT
Re execc's comment: Yes, refreshChildren() is being called. I've tested this by overriding it and adding a simple System.err line, which is being displayed on the console on deletion of a node:
#Override
public void refreshChildren() {
super.refreshChildren();
System.err.println("refreshChildren() IS being called!");
}
EDIT 2
The funny (well...) thing is, when I close the editor and persist the model, then re-open the same file, the node isn't painted anymore, and is not present in the model. But what does this mean? Am I working on a stale model? Or is refreshing/getting the model children not working properly?
EDIT 3
I've just found a peculiar thing, which might explain the isues I have? In the getModelChildren() method I call allModelObjects.addAll(((Diagram) getModel()).getMyNodes());, and getMyNodes() returns an unmodifiable EList. I found out when I tried to do something along the lines of ((Diagram) getModel()).getMyNodes().remove(getMyNode()) in the delete command, and it threw an UnsupportedOperationException... Hm.
EDIT 4
Er, somebody kill me please?
I've double-checked whether I'm handling the same Diagram object at all times, and while doing this I stumbled across a very embarassing thing:
The getModelChildren() method in DiagramEditPart in the last version read approx. like this:
#Override protected List<EObject> getModelChildren() {
List<EObject> allModelObjects = new ArrayList<EObject>();
EList<MyNode> nodes = ((Diagram) getModel()).getMyNodes();
for (MyNode node : nodes) {
if (node.getDiagram() != null); // ### D'Uh! ###
allModelObjects.add(node);
}
return allModelObjects;
}
I'd like to apologize for stealing everyone's time! Your suggestions were very helpful, and indeed helped my to finally track down the bug!
I've also learned a number of lessons, amongst them: Always paste the original code, over-simplifaction may cloak your bugs! And I've learned a lot about EMF, Adapter, and GEF. Still:
There is one semi-colon too many in line 5 of the following part of the code, namely after the if statement: if (node.getDiagram() != null);:
1 #Override protected List<EObject> getModelChildren() {
2 List<EObject> allModelObjects = new ArrayList<EObject>();
3 EList<MyNode> nodes = ((Diagram) getModel()).getMyNodes();
4 for (MyNode node : nodes) {
5 if (node.getDiagram() != null);
6 allModelObjects.add(node);
7 }
8 return allModelObjects;
9 }