Flutter Error: "Widget cannot build because is already in the process of building" - widget

I got this error:
I/flutter (29346): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (29346): The following assertion was thrown building MainLogic(dirty, state: _MainLogic#9c794):
I/flutter (29346): setState() or markNeedsBuild() called during build.
I/flutter (29346): This InhWidget widget cannot be marked as needing to build because the framework is already in the
I/flutter (29346): process of building widgets. A widget can be marked as needing to be built during the build phase
I/flutter (29346): only if one of its ancestors is currently building. This exception is allowed because the framework
I/flutter (29346): builds parent widgets before children, which means a dirty descendant will always be built.
I/flutter (29346): Otherwise, the framework might not visit this widget during this build phase.
I/flutter (29346): The widget on which setState() or markNeedsBuild() was called was:
I/flutter (29346): InhWidget(state: InhState#3aaa8)
I/flutter (29346): The widget which was currently being built when the offending call was made was:
I/flutter (29346): MainLogic(dirty, state: _MainLogic#9c794)
Before flooding you with code I'd like to explain a bit the logic behind:
I'm trying to implement the rules of the game baccarat,
to do so I set up an Inherited widget, some stateless widget send
the input to an Object stored in the inherited state,
according to this object data other widget rebuild themselves.
the last change I made is applying the baccarat "drawing rules",
that can make the Object skip an input when required,
but when this happens the inherited widget doesn't like it.
I'll post the last part of the code, but if you think that
something else is relevant just ask and I'll post it.
thank you in advance for you help
class MainLogic extends StatefulWidget {
#override
State<StatefulWidget> createState() => new _MainLogic();
}
class _MainLogic extends State<MainLogic> {
Widget cardGrid = CardGrid();
Widget newGame = NewGame();
Rules rules;
bool gameOver;
#override
void initState(){
super.initState();
gameOver = false;
}
#override
Widget build(BuildContext context) {
final InhState state = InhWidget.of(context);
rules = new Rules(game:state.game);
if(gameOver==false && state.game.count>3 && rules.playerExtraCard()==false)
{state.skip();}
if(gameOver==false && state.game.count>4 && rules.bankerExtraCard()==false)
{state.skip();}
checkGame(state.game.count);
return Container(child:status(gameOver));
}
status(bool _gameOver) {
Widget whichOne;
if (_gameOver) {whichOne=newGame;}
else {whichOne=cardGrid;}
return whichOne;
}
void checkGame(int cards) {
if (cards >= 6) {
gameOver = true;
} else {gameOver = false;}
}
...
class Rules {
final Game game;
Rules({this.game});
static Deck deck = new Deck();
int get p1 => deck.getValue(game.p1);
int get p2 => deck.getValue(game.p2);
int get p3 => deck.getValue(game.p3);
int get b1 => deck.getValue(game.b1);
int get b2 => deck.getValue(game.b2);
int get b3 => deck.getValue(game.b3);
normalize(List<int> input) {
int normal;
int initial = 0;
input.forEach((num e){initial += e;});
if(initial>9) {normal=initial-10;}
else {normal=initial;}
return normal;
}
playerExtraCard() {
bool extraCard;
int pInit = normalize([p1,p2]);
int bInit = normalize([b1+b2]);
if (pInit > 5) {extraCard = false;}
else if (bInit > 7) {extraCard = false;}
else {extraCard = true;}
return extraCard;
}
bankerExtraCard() {
bool extraCard;
int pInit = normalize([p1,p2]);
int bInit = normalize([b1+b2]);
int pWing = normalize([p3]);
if (pInit>7) {extraCard = false;}
else if (bInit>6) {extraCard = false;}
else if (pInit>5 && bInit<6) {extraCard = true;}
else if (pInit<6 && bInit==6 && pWing<8 && pWing>5) {extraCard = true;}
else if (pInit<6 && bInit==5 && pWing<8 && pWing>3) {extraCard = true;}
else if (pInit<6 && bInit==4 && pWing<8 && pWing>1) {extraCard = true;}
else if (pInit<6 && bInit==3 && pWing==8) {extraCard = true;}
else if (pInit<6 && bInit<3) {extraCard = true;}
else {extraCard = false;}
return extraCard;
}
}
...
class InhState extends State<InhWidget> {
Game game = new Game();
#override
void initState() {
super.initState();
game.blank();
}
void deal(String card) {
setState(() {
game.cards[game.count] = card;
game.count = game.count + 1;
});
}
void restart(){
setState(() {
game.blank();
});
}
void skip() {
setState(() {
game.count = game.count + 1;
});
}
#override
Widget build(BuildContext context) {
return new InhCore(
data: this,
child: widget.child,
);
}
}
...
class InhWidget extends StatefulWidget {
InhWidget({this.child});
final Widget child;
#override
State<StatefulWidget> createState() => InhState();
static InhState of(BuildContext context) {
return (context.inheritFromWidgetOfExactType(InhCore) as InhCore).data;
}
}

You call state.skip() in build() and setState() in skip()
void skip() {
setState(() {
game.count = game.count + 1;
});
}
Usually it's a good idea to avoid "business logic" in build().
There shouldn't be a need to calculate all that stuff that you do using InhState in build(). build() can be called very often at various times.
You should rather update the state depending on user interaction events (onTap:, ...) or other event sources like streams or futures that indicate state changes from async operations.

I'd side with #Günter Zöchbauer's answer and refactor the code as he advises, but for quicker fix you can try to wrap the state.skip(); instructions (the ones inside the build()) into a WidgetsBinding.instance.addPostFrameCallback. This way you'd defer the state change operation to happen right after the build.
#override
Widget build(BuildContext context) {
final InhState state = InhWidget.of(context);
rules = new Rules(game:state.game);
if (!gameOver) {
if (state.game.count > 3 && !rules.playerExtraCard() ||
state.game.count > 4 && !rules.bankerExtraCard())
{
WidgetsBinding.instance.addPostFrameCallback((_) {
state.skip();
});
}
}
checkGame(state.game.count);
return Container(child:status(gameOver));
}
Unrelated, but I'd also advise you to use flutter format on your project to help you with your code style.

Related

How to update/setState bool value through a method's input in flutter?

This one probably has a very easy answer, but I've been blocked trying to solve this for a while and can't really get it to work.
**problem:**when I call checkBeans() with the input got1Beans on the initState, I still get the initial value (false) for got1Beans. What I need is to be able to update got1Beans = true. Anyone able to sort this beany situation? What am I missing here?
import 'package:flutter/material.dart';
class BeansExample extends StatefulWidget {
#override
_BeansExampleState createState() => _BeansExampleState();
}
class _BeansExampleState extends State<BeansExample> {
bool got1beans = false;
bool got2beans = false;
bool got3beans = false;
bool got4beans = false;
bool got5beans = false;
checkBeans(bool getBeans) {
setState(() {
getBeans = true; //shouldn't it update the input value???
});
}
#override
void initState() {
super.initState();
checkBeans(got1beans); //here I call got1Beans
print(got1beans); //This prints false, need to get true here
}
#override
Widget build(BuildContext context) {
return Container();
}
}
checkBeans(value) doesn't mean this.gotXbeans = true/false. It means value = true/false. The variable "value" should be local or method variable.
You can update your code.
List<bool> beans = [false, false, false, false, false];
checkBeans(int index, bool value) {
setState(() {
beans[index] = value;
});
}
Or you can create methods like this.
checkBeans1, checkBeans2, checkBeans3, checkBeans4, checkBeans5...
checkBeans1(bool value) {
setState(() {
get1beans = value;
});
}
Please check this question if you want to pass a reference.

ViewPart hangs on click of JButton

I am working on Teamcenter RAC customization. I have changed an existing code which deals with viewpart and jbuttons on it. The viewpart(SWT) loads a stylesheet rendering panel. the problem is whenever I click on the save button (JButton) this hangs the teamcenter application on post -executing activities.
The code is as follows:
saveCheckOutButton.addActionListener( new ActionListener()
{
#Override
public void actionPerformed( ActionEvent paramAnonymousActionEvent )
{
final AbstractRendering sheetPanel = itemPanel.getStyleSheetPanel();
final AbstractRendering sheetPanel1 = itemRevPanel.getStyleSheetPanel();
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
#Override
protected Void doInBackground()
throws Exception
{
if(pPanel==null)
return null;
if( pPanel.isPanelSavable())
{
if(sheetPanel==null|| sheetPanel1==null)
return null;
sheetPanel.saveRendering();
sheetPanel1.saveRendering();
/*if(!sheetPanel.getErrorFlag() && !sheetPanel1.getErrorFlag())
{
sheetPanel.setModifiable( false );
sheetPanel1.setModifiable( false );
}*/
}
return null;
}
#Override
protected void done(){
if(!sheetPanel.getErrorFlag() && !sheetPanel1.getErrorFlag())
{
sheetPanel.setModifiable( false );
sheetPanel1.setModifiable( false );
}
}
};
worker.execute();
}
} );
I have written the code under swingworker as suggested by some of the experts here but to no success. Request for some immediate help.
What do you mean by "it hangs the teamcenter application". Whether it responds too slow or doInBackground() is not properly executed?
Anyway you can try executing your rendering code in SwingUtilities.invokeLater() and use the method get(). If you don't call get() in the done method, you will lose all the exceptions that the computation in the doInBackground() has thrown. So we will get to know about exception if any is there.
SwingUtilities.invokeLater() allows a task to be executed at some later point in time, as the name suggests; but more importantly, the task will be executed on the AWT event dispatch thread. Refer Invoke later API documentation for the detailed info.
About get():
Waits if necessary for the computation to complete, and then retrieves its result.
Note: calling get on the Event Dispatch Thread blocks all events, including repaints, from being processed until this SwingWorker is complete.
saveCheckOutButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent paramAnonymousActionEvent) {
final AbstractRendering sheetPanel = itemPanel.getStyleSheetPanel();
final AbstractRendering sheetPanel1 = itemRevPanel.getStyleSheetPanel();
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
#Override
protected Void doInBackground() throws Exception {
if (pPanel == null)
return null;
if (pPanel.isPanelSavable()) {
if (sheetPanel == null || sheetPanel1 == null)
return null;
saveRendering();
}
return null;
}
#Override
protected void done() {
try {
get();
if (!sheetPanel.getErrorFlag() && !sheetPanel1.getErrorFlag()) {
sheetPanel.setModifiable(false);
sheetPanel1.setModifiable(false);
}
} catch (final InterruptedException ex) {
throw new RuntimeException(ex);
} catch (final ExecutionException ex) {
throw new RuntimeException(ex.getCause());
}
}
};
worker.execute();
}
});
private void saveRendering() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
sheetPanel.saveRendering();
sheetPanel1.saveRendering();
}
});
}

CustomResourceHandler -> Version number is added to resources twice

I've written an CustomResourceHandler to add a version number to my js and css files, which works fine, but all primefaces resources get a double version number now.
Without CustomResourceHandler:
javax.faces.resource/jquery/jquery-plugins.js.xhtml?ln=primefaces&v=6.0.17
With CustomResourceHandler:
javax.faces.resource/jquery/jquery-plugins.js.xhtml?ln=primefaces&v=6.0.17&v=6.0.17
My CustomResourceHandler:
public class ExtendedResourceHandler extends PrimeResourceHandler {
public ExtendedResourceHandler(ResourceHandler wrapped) {
super(wrapped);
}
#Override
public Resource createResource(String resourceName, String libraryName) {
if (!org.primefaces.util.Constants.LIBRARY.equalsIgnoreCase(libraryName)
&& !org.primefaces.extensions.util.Constants.LIBRARY.equalsIgnoreCase(libraryName) && !"javax.faces".equalsIgnoreCase(libraryName) && resourceName != null
&& (resourceName.endsWith(".css") || resourceName.endsWith(".js"))) {
final Resource resource = super.createResource(resourceName, libraryName);
if (resource == null) {
return null;
}
return new ResourceWrapper() {
#Override
public String getRequestPath() {
String resultPath = super.getRequestPath();
resultPath += (resultPath.contains("?") ? "&" : "?") + "version=V6326";
return resultPath;
}
#Override
public Resource getWrapped() {
return resource;
}
};
} else {
return super.createResource(resourceName, libraryName);
}
}
}
faces-config.xml:
<resource-handler>de.sync4.cockpit.web.beans.resourcehandler.ExtendedResourceHandler</resource-handler>
Because of that problem I can't get the pe:ckEditor to work, because it can't find necessary resources. Any idea what's wrong with my Handler?
Found the Problem. I needed to extend the ResourceHandlerWrapper, not the PrimeResourceHandler.
I also have an ExtendedBeanELResolver. I had to return null there if the base object is an instance of my custom handler.

GeoDataApi.getAutocompletePredictions not working

I am building an android application that shows autocomplete feature and fetches autocomplete predictions in google maps using - GeoDataApi.getAutocompletePredictions. I followed this tutorial - https://github.com/googlesamples/android-play-places/blob/master/PlaceComplete/Application/src/main/java/com/example/google/playservices/placecomplete/PlaceAutocompleteAdapter.java
But somehow this is not working fine for me.
My class is this -
public class GooglePlacesAutoCompleteAdapter extends ArrayAdapter implements Filterable {
private ArrayList<PlaceAutocomplete> mResultList;
GoogleApiClient mGoogleApiClient;
private LatLngBounds mBounds;
private AutocompleteFilter mPlaceFilter;
int radius = 500;
public GooglePlacesAutoCompleteAdapter(Context context, int textViewResourceId, GoogleApiClient googleApiClient,
Location lastLocation, AutocompleteFilter filter) {
super(context, textViewResourceId);
LatLng currentLatLng = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
mBounds = Utility.boundsWithCenterAndLatLngDistance(currentLatLng, 500, 500);
mGoogleApiClient = googleApiClient;
mPlaceFilter = filter;
}
#Override
public int getCount() {
return mResultList.size();
}
#Override
public PlaceAutocomplete getItem(int index) {
return mResultList.get(index);
}
#Override
public android.widget.Filter getFilter() {
Filter filter = new Filter() {
#Override
public FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null && constraint.length() > 3 && constraint.length()%3 == 1) {
// Retrieve the autocomplete results.
mResultList = autocomplete(constraint.toString());
// Assign the data to the FilterResults
filterResults.values = mResultList;
filterResults.count = mResultList.size();
}
return filterResults;
}
#Override
public void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
public ArrayList<PlaceAutocomplete> autocomplete(String input) {
if (mGoogleApiClient.isConnected()) {
// Submit the query to the autocomplete API and retrieve a PendingResult that will
// contain the results when the query completes.
PendingResult results = Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, input.toString(),
mBounds, mPlaceFilter);
// This method should have been called off the main UI thread. Block and wait for at most 60s
// for a result from the API.
AutocompletePredictionBuffer autocompletePredictions = (AutocompletePredictionBuffer)results.await(60, TimeUnit.SECONDS);
// Confirm that the query completed successfully, otherwise return null
final Status status = autocompletePredictions.getStatus();
if (!status.isSuccess()) {
//Toast.makeText(getContext(), "Error contacting API: " + status.toString(),Toast.LENGTH_SHORT).show();
//Log.e(TAG, "Error getting autocomplete prediction API call: " + status.toString());
autocompletePredictions.release();
return null;
}
// Copy the results into our own data structure, because we can't hold onto the buffer.
// AutocompletePrediction objects encapsulate the API response (place ID and description).
Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
ArrayList resultList = new ArrayList<>(autocompletePredictions.getCount());
while (iterator.hasNext()) {
AutocompletePrediction prediction = iterator.next();
// Get the details of this prediction and copy it into a new PlaceAutocomplete object.
resultList.add(new PlaceAutocomplete(prediction.getPlaceId(), prediction.getDescription()));
}
// Release the buffer now that all data has been copied.
autocompletePredictions.release();
return resultList;
}
//Log.e(TAG, "Google API client is not connected for autocomplete query.");
return null;
}
class PlaceAutocomplete {
public CharSequence placeId;
public CharSequence description;
PlaceAutocomplete(CharSequence placeId, CharSequence description) {
this.placeId = placeId;
this.description = description;
}
#Override
public String toString() {
return description.toString();
}
}
}
The line on which GeoDataApi.getAutocompletePredictions is called, goes into an internal classes called - Filter.java, Log.java, handler.java and then Looper.java and loops there indefinetly on line 121 of Looper.java (I am sure studio sdk will show the code for Looper.java).
It is not even throwing an error, or going to the next line, it just does not work. Plus, I am not able to see the stack trace of an error.
This is the code snippet which is calling this -
if (mLastLocation != null) {
GooglePlacesAutoCompleteAdapter placesAdapter = new GooglePlacesAutoCompleteAdapter(this, R.layout.item_list, mGoogleApiClient, mLastLocation, null);
autoCompView.setAdapter(placesAdapter);
autoCompView.setOnItemClickListener(this);
}
Can someone please tell me what I am doing wrong here? Please any help will be greatly appreciated. I need to get this working as soon as I could.
PS - I am passing mPlaceFilter as null here.
Enable the Google Places API for Android in developers console

What's your most reused class?

Every programmer ends up with a set of utility classes after a while. Some of them are true programming pearls and they are reused in several of your projects. For example, in java:
class Separator {
private String separator;
private boolean called;
public Separator(String aSeparator) {
separator = aSeparator;
called = false;
}
#Override
public String toString() {
if (!called) {
called = true;
return "";
} else {
return separator;
}
}
}
and:
public class JoinHelper {
public static <T> String join(T... elements) {
return joinArray(" ", elements);
}
public static <T> String join(String separator, T... elements) {
return joinArray(separator, elements);
}
private static <T> String joinArray(String sep, T[] elements) {
StringBuilder stringBuilder = new StringBuilder();
Separator separator = new Separator(sep);
for (T element : elements) {
stringBuilder.append(separator).append(element);
}
return stringBuilder.toString();
}
}
What is your most reused class?
System.Object - almost all my types extend it.
A utility class that has logging and email functionality. An extensions class that contains extension methods. A reporting class that basically harness the reporting services web service and makes it easy to stream reports as excel, pdf, etc.
Examples...
1.) Utility Class (static)
public static void LogError(Exception ex)
{
EventLog log = new EventLog();
if (ex != null)
{
log.Source = ConfigurationManager.AppSettings["EventLog"].ToString();
StringBuilder sErrorMessage = new StringBuilder();
if (HttpContext.Current.Request != null && HttpContext.Current.Request.Url != null)
{
sErrorMessage.Append(HttpContext.Current.Request.Url.ToString() + System.Environment.NewLine);
}
sErrorMessage.Append(ex.ToString());
log.WriteEntry(sErrorMessage.ToString(), EventLogEntryType.Error);
}
}
2.) Extensions Class
public static IEnumerable<TSource> WhereIf<TSource>(this IEnumerable<TSource> source, bool condition, Func<TSource, bool> predicate)
{
if (condition)
return source.Where(predicate);
else
return source;
}
public static short getLastDayOfMonth(short givenMonth, short givenYear)
{
short lastDay = 31;
switch (givenMonth)
{
case 4:
case 6:
case 9:
case 11:
lastDay = 30;
break;
case 2:
if ((int)givenYear % 4 == 0)
{
lastDay = 29;
}
else
{
lastDay = 28;
}
break;
}
return lastDay;
}
Most reused but boring:
public static void handleException(Exception e) throws RuntimeException {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new RuntimeException(e); //NOPMD
}
Less boring (also methods for building lists and sets):
/**
* Builds a Map that is based on the Bean List.
*
* #param items Bean List items
* #param keyField Bean Field that will be key of Map elements (not null)
* #return a Map that is based on the Bean List
*/
#SuppressWarnings("unchecked")
public static <T, K> Map<K, T> buildMapFromCollection(final Collection<T> items,
boolean linkedMap,
final String keyField,
final Class<K> keyType) {
if (items == null) {
return Collections.emptyMap();
}
if (keyField == null) {
throw new IllegalArgumentException("KeyField is null");
}
final Map<K, T> result;
if (linkedMap) {
result = new LinkedHashMap<K, T>();
} else {
result = new HashMap<K, T>();
}
BeanMapper mapper = null;
for (final T item : items) {
if (mapper == null) {
mapper = new BeanMapper(item.getClass());
}
final K key = (K) mapper.getFieldValue(item, keyField);
result.put(key, item);
}
return result;
}
Logger class: Which logs the flow of control in a log file.
Configuration Reader/Setter: which reads the configuration from ini/xml file and sets the environment of the application
Most reused? Hmmm...
boost::shared_ptr<> with boost::weak_ptr<>
probably most reused (also probably most bang-for-buck ratio)
Globals
Just a simple class with static DBConnString, and a few other app wide settings.
Have reused the simple file in about 2 dozen projects since working with .Net
A ConcurrentDictionary I wrote, which I now seem to use everywhere (I write lots of parallel programs)